简体   繁体   English

如何在反应中转换日期?

[英]How to convert date in react?

From 2018-12-14 to 2018/12/14???从 2018-12-14 到 2018/12/14???
I usually miss one day when i send date(datepicker(momentjs))当我发送日期(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"; startDate = "2018-12-14"; momentDate = moment(startDate ,'YYYY/MM/DD') 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要处理时刻,您需要使用 format("YYYY/MM/DD")时刻

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.所以,如果你真的想用moment来做,你有两种方法。

  1. If you already have formatted startDate and want to reformat it如果您已经格式化了 startDate 并且想要重新格式化它

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

  1. If you are creating new startDate with proper format如果您正在以正确的格式创建新的 startDate

 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结果日期:2018/12/14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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