0

The following code is inside return of render function

this.props.investment.selectedInvestor.rounds.map((item,index)=>{
   if(item.type.toLowerCase()==this.state.securityType.toLowerCase())
   {
      return(
        <tr onClick={()=>this.showRoundDetails(true,item)}>
        <td>
          <strong>{item.round_name}</strong><br/>
          <span>{item.close_date}</span>
          </td>
        <td>{item.security_type}</td>
        <td>{item.sharing_plan}</td>
        <td>{item.investments[0].status}</td>
        <td>{item.rate}</td>
        <td>{item.frequency}</td>
        <td>{item.investments[0].current_amount}</td>
        </tr>
        )  
    }                             
  }

I want to convert {item.close_date} to another format i.e use this function toLocaleDateString()
I tried declaring another variable outside return and then convert the date but still could not solve it

5
  • whats the format of item.close_date? Are you OK with using external libraries? Commented May 12, 2017 at 7:54
  • if item.close_data is a Date object then you can use that method directly like this: <span>{item.close_date.toLocaleDateString()}</span> Commented May 12, 2017 at 7:56
  • {item.close_date} is a string Commented May 12, 2017 at 7:58
  • Can you show example of it? Commented May 12, 2017 at 8:00
  • 2017-05-10T11:01:50.569Z is the value of that object Commented May 12, 2017 at 8:01

2 Answers 2

9

{item.close_date} is a string (that contains the date) as you mentioned in comment, to use toLocaleDateString() method first you need to convert that string into date object by using new Date(), after that you can use any date method on that.

Use this:

<span>{ (new Date(item.close_date)).toLocaleDateString() }</span>
Sign up to request clarification or add additional context in comments.

Comments

1

Using JavaScript date method:

<span>{ (new Date(item.close_date)).toLocaleDateString() }</span>

var d =  new Date("2017-05-10T11:01:50.569Z");
console.log(d.getDay(), d.getMonth(), d.toLocaleDateString())

Using MomentJS library

<span> 
    { 
       moment("2017-05-10T11:01:50.569Z", "YYYY-MM-DDThh:mm:ss.SSS").format("hh:mm a") 
    }
</span>

var d = moment("2017-05-10T11:01:50.569Z", "YYYY-MM-DDThh:mm:ss.SSS")
console.log(d.format("hh:mm a"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

I prefer momentjs because I can parse and get date in any format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.