Get the given date format (the string specifying the format) in javascript or momentjs

前端 未结 2 1627
遥遥无期
遥遥无期 2020-12-06 18:04

Given a datestring, how can I get the format string describing that datestring?

Put another way, how can I get the format string that Date() or MomentJS (might be di

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 18:30

    Consolidating information from Matt Johnson's answer, some comments, and my own contribution.

    With Moment.js (version 2.10.7+), you can use the Creation Data API. Something like this in Node.js:

    moment('2016-01-01 00:00:00').creationData().format
    

    outputs

    'YYYY-MM-DD HH:mm:ss'
    

    Just as any date parsing is, there is ambiguity about the format of many datestrings due to things such as locale (the order of months and days are switched between the US and Europe, for example). But the above method is sufficient for me.

提交回复
热议问题