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 ?

Or Assayag
  • 5,662
  • 13
  • 57
  • 93
Rezabk
  • 1
  • Is `startDate`string or Date? – palindrom Sep 01 '21 at 08:48
  • Does this answer your question? [Format JavaScript date as yyyy-mm-dd](https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd) – palindrom Sep 01 '21 at 08:51
  • I recommend using http://momentjs.com/ for date parsing and formatting. console.log(moment(Date.parse("1400-07-07T00:00:00.000Z")).format("YYYY-MM-DD")) – null Sep 01 '21 at 09:02

1 Answers1

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);
fravolt
  • 2,565
  • 1
  • 4
  • 19