最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert date in react? - Stack Overflow

programmeradmin1浏览0评论

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')??? 

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')??? 
Share Improve this question edited Dec 14, 2018 at 7:13 Narendra Jadhav 10.3k15 gold badges34 silver badges44 bronze badges asked Dec 14, 2018 at 7:11 AlexiostigrahaudaAlexiostigrahauda 171 gold badge1 silver badge8 bronze badges 2
  • 2 Please spend some time framing the question properly so that the munity will find it easy to help you out. I am not talking about english, I am talking about the framing. – Harsha Venkataramu Commented Dec 14, 2018 at 7:14
  • Please refer the moment doc moment().format('YYYY/MM/DD') – Narendra Jadhav Commented Dec 14, 2018 at 7:17
Add a ment  | 

6 Answers 6

Reset to default 4
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

Use format:

Data: "createdAt": "2022-04-04T18:56:36Z",

import { format } from "date-fns";

{format(new Date(reserve.createdAt), "dd/MM/yyyy HH:mm:ss")}

Result:

04/04/2022 18:56:36

Just be careful with MM and mm, MM is for month and mm is for minute

发布评论

评论列表(0)

  1. 暂无评论