Datetime format specifiers

时间:2015-09-14 16:05:26

标签: javascript jquery

Simple question: is there a standard for datetime format specifiers? I know what MS specifies for .Net, but we have some javascript controls that seem to work differently. I refer specifically to case, where .Net (for example) uses 'M' for month and 'm' for minutes, whereas a JQuery control we use clearly requires 'm' for the numeric month (eg mm/dd/yyyy) and converts MM to a short month name (eg Jan). Is the JQuery control incorrect, or outside .Net is it a case of anything goes?

1 个答案:

答案 0 :(得分:2)

As @james-thorpe points out, standards aren't really standard.

That said: JavaScript does not inherently have a string formatting standard. It does - however - understand a few formats implicitly. Namely: RFC2822 and ISO8601.

The unspoken answer here is that no, it's not "anything goes" outside of .NET*, it's just that - again - standards vary.

jQuery has chosen m for month, and you'll just have to convert formats to the front end.

For what it's worth, MomentJS is a beautiful, amazing front end library built for dealing with dates. You can pass it your known format:

moment( dotNetDate, '[.NET format]' );

...and get back a "Moment in time" object that you can then format in any way:

myMomentInTime.format( '[an arbitrary format]' );

I hope this helps a bit.

Note: for what it's worth, m is not usually 'month'. m is almost always 'minute', so it's probably worth noting here that jQuery is either in a small minority or they are explicitly not supporting time formats. It's probably the latter, based on nothing more than a guess.


Update:

I should point out that I glossed over some of the question text and mentally converted

a JQuery control we use

to

jQuery

It is altogether possible that, if this control is not a jQuery-owned component, it is implementing it's own 'standard' on top of jQuery. It's been a long time since I've used any jQuery UI components, so it's hard for me to tell if they have their own standardized date format or not. If it is their own, my previous comments still apply. If it is a third party, my comments still apply, just to that third party and not to jQuery itself.

End Update


* It's my personally held belief that the opposite is true in modern web development: .NET chooses their own thing which doesn't coincide with many/any known standards. You can do it the .NET way, or you can do it way everyone else does it. This is my opinion based on my interactions with and development using .NET, and opinions - as always - differ.