在javascript或momentjs中获取给定的日期格式(指定格式的字符串) [英] Get the given date format (the string specifying the format) in javascript or momentjs

查看:497
本文介绍了在javascript或momentjs中获取给定的日期格式(指定格式的字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个日期字符串,如何获取描述该日期字符串的格式字符串?

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

换句话说,我怎样才能获得Date()或MomentJS的格式字符串(如果没有通过显式格式使用,那么可能会用来解析那个日期字符串吗?

Put another way, how can I get the format string that Date() or MomentJS (might be different for each, that's fine) would use to parse that datestring if one didn't pass an explicit format to use?

所以给定'2016-01-01'例如,它应输出类似'YYYY-MM-DD'的内容。

So given '2016-01-01' it should output something like 'YYYY-MM-DD', for example.

(我知道这是一个简单的问题,可能会在某个地方找到答案,但很难简明扼要地说,所以我只能找到关于如何解析约会条带或如何显示的问题和答案日期。没有关于如何输出格式本身。)

(I am aware this is a simple question and may have an answer somewhere, but it is difficult to word concisely, so I could only find questions and answers about how to parse datestrings or how to display dates. None about how to output the format itself.)

推荐答案

整合来自马特约翰逊的回答,一些评论和我自己的贡献。

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

随着时间的推移.js(版本2.10.7+),你可以使用 Creation Data API 。在Node.js中有类似的东西:

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

输出

'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.

这篇关于在javascript或momentjs中获取给定的日期格式(指定格式的字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆