0

I have a table like this

<table className="table terms-table">
                        <thead>
                            <tr>                                   
                                <th scope="col" >   start Date</th>
                                <th scope="col" >   end Date </th>

                           </tr>
                        </thead>
                        <tbody>
                            {termsData.map(term =>
                            (
                                <tr key={term._id}>
                                                                                                                             
                                   <td>
                                        {term.startDate}
                                    </td>
                                    <td>
                                        {term.endDate}
                                    </td>
                                   
                                </tr>
                            ))}
                        </tbody>
                    </table>

and the date be like 1400-07-07T00:00:00.000Z

i just want 1400-07-07

how should i do this ?

3
  • Is startDatestring or Date? Commented Sep 1, 2021 at 8:48
  • Does this answer your question? Format JavaScript date as yyyy-mm-dd Commented Sep 1, 2021 at 8:51
  • I recommend using momentjs.com for date parsing and formatting. console.log(moment(Date.parse("1400-07-07T00:00:00.000Z")).format("YYYY-MM-DD")) Commented Sep 1, 2021 at 9:02

1 Answer 1

0

The simplest way to do this (assuming date is a String) is to use the String's substring function.

let date = "1400-07-07T00:00:00.000Z";
let newDate = date.substring(0, 10);
console.log('date is', date);
console.log('new date is', newDate);

If your date is already a javascript Date object, you can use the appropriate getters to get the data you want.

let date = new Date();
let dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
console.log(dateString);

Sign up to request clarification or add additional context in comments.

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.