简体   繁体   中英

Using React-Moment, (moment.js) How do I get date format: Mon Apr 19 1976 09:59:00 from format 2017-08-07 08:42:08?

I'm trying to convert the date format 2017-08-07 08:42:08 to format Mon Apr 19 1976 0:59:00 using react-moment library.

I went through the docs at [https://www.npmjs.com/package/react-moment][1] and tried similar examples, but nothing fits my need exactly.

I can do:

<Moment fromNow>{datetime}</Moment>

yields format:

in 10 days

But

<Moment unix>{datetime}</Moment>

yields

Invalid date

<Moment parse="YYYY-MM-DD HH:mm:ss">
    {datetime}
</Moment>

yields: Mon Aug 07 2017 08:47:47 GMT+0545 . I don't it without the timezone value at the end. What's the correct parameter for this date format from this input?

Moment can parse most standard date formats. Use the parse attribute to tell moment how to parse the given date when non-standard.

https://github.com/headzoo/react-moment#parsing-dates

You can set the output in whatever format you want. Check out the table of possible tokens . Choose the tokens you need and set them on the format attribute. Here's some examples:

<Moment
  format="YYYY-M-D H:m:s"
  parse="YYYY-MM-DD HH:mm:ss"
>2017-08-18 11:01:21</Moment>

With localization:

<Moment
  format="LL LT"
  parse="YYYY-MM-DD HH:mm:ss"
>2017-08-18 11:01:21</Moment>

With time from now:

<Moment
  fromNow
  parse="YYYY-MM-DD HH:mm:ss"
>2017-08-18 11:01:21</Moment>

Sandbox example .

As per documentation in npm default format that you want. Because that default format of js, as mentioned in w3schools

<Moment format="Here desire format">{datetime}</Moment>

*optional: Please try to install moment-timezone as mentioned in the link.

Here I have done it using momentjs library

code

output

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