简体   繁体   中英

How to convert date in react?

From 2018-12-14 to 2018/12/14???
I usually miss one day when i send date(datepicker(momentjs))

startDate: moment().toDate(), // how to format('YYYY/MM/DD')??? 
moment().format("YYYY/MM/DD"); // if you want to convert current date
moment(startDate).format("YYYY/MM/DD"); // if you want to convert given date

startDate = "2018-12-14"; momentDate = moment(startDate ,'YYYY/MM/DD')

If you know the exact format you can put it here .

For doing with moment you need to use format("YYYY/MM/DD") moment

In case you want to do it manually

If you just want to change - to / than you can do it like this.

 let str = "2018-12-14"; let op = str.replace(/-/g,'/'); console.log(op);

You can just replace - to / if you have it already formatted but from the example, you gave us it appears that you don't know how to format date at all. So if you really want to do it with moment you've got 2 approaches.

  1. If you already have formatted startDate and want to reformat it

 const reformattedStartDate = moment(startDate).format('YYYY/MM/DD');

  1. If you are creating new startDate with proper format

 const startDate = moment().format('YYYY/MM/DD');

Import moment from 'moment';

let date = 2018-12-14;

let resultDate=moment(date).format('YYYY/MM/DD');

Result:

resultDate: 2018/12/14

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM