简体   繁体   English

格式已删除ISO 8601日期

[英]Format Elided ISO 8601 Date

I have an ISO date string in a javascript variable, and I want to format it for display. 我在javascript变量中有一个ISO日期字符串,我想对其进行格式化以进行显示。 Simply enough, you might think. 简单地说,您可能会认为。 For instance: 例如:

new Date(iso).toDateString()

However, ISO dates might only represent a year ("yyyy"), or a year and month ("yyyy-mm"); 但是,ISO日期只能代表年份(“ yyyy”)或年份和月份(“ yyyy-mm”); they're not always "yyyy-mm-dd". 他们并不总是“ yyyy-mm-dd”。 Under these circumstances, the parse method fills in defaults for the missing fields, which I do not want to see. 在这种情况下,parse方法将为缺少的字段填写默认值,我不想看到这些默认值。

In other words, I want to only format the date fields defined in the ISO string, but no others. 换句话说,我只想格式化在ISO字符串中定义的日期字段,而没有其他格式。 If the ISO string was "2017-12" then I may want it formatted as one of 12/2012, Dec 2017, December 2017, décembre 2017, depending on my locale and level of detail; 如果ISO字符串为“ 2017-12”,那么我可能希望将其格式化为12 / 2012、2017年12月,2017年12月,2017年12月13日之一,具体取决于我的语言环境和详细程度; I would not want a day number. 我不希望有天数。 Similarly, if the ISO string was only "2017" then I would not want a day or month indication. 同样,如果ISO字符串仅为“ 2017”,那么我就不需要日期或月份指示。

This question is primarily related to the elided ISO forms, and is therefore not a duplicate. 这个问题主要与被淘汰的ISO表单有关,因此不是重复的问题。

Is there a simple method of formatting such ISO dates properly -- preferably in a locale-aware fashion -- or do I have to do it all long-hand? 是否有一种简单的方法来正确格式化此类ISO日期-最好以一种可识别语言环境的方式进行格式化-还是我必须长期这样做?

If you are using JavaScript's native Date type at any point of the processing, then you're stuck; 如果在处理的任何时候都使用JavaScript的本机Date类型,那么您将陷入困境; that object type can only store an instant in time, represented internally as a timestamp ( absolute count of milliseconds since the UNIX epoch, not counting leap seconds ). 该对象类型只能存储一个即时时间,在内部以时间戳记表示( 自UNIX时代以来的绝对毫秒数,不包括leap秒 )。 The various get * methods just calculate values based on the stored timestamp, and the set * methods calculate a new timestamp based on the requested adjustment. 各种get *方法仅基于存储的时间戳来计算值,而set *方法则基于请求的调整来计算新的时间戳。 If you set anything to NaN or undefined , then the timestamp itself becomes NaN and all the get * methods will likewise return NaN . 如果将任何内容设置为NaNundefined ,则时间戳本身将变为NaN并且所有get *方法将同样返回NaN

If you're dealing with dates and times with uncertain or un(der)specified components you might be best off steering clear of Date entirely; 如果您要处理不确定的或未指定的组件的日期和时间,则最好完全避免使用Date just use a generic Object with only the fields that have defined values, and write your own parser and formatter to match. 只需使用仅具有定义值的字段的通用对象,然后编写自己的解析器和格式化程序即可匹配。 If you can extract the relevant part of the string, you might still be able to use the native Date methods and objects in passing to handle the tricky bits like translating possibly-abbreviated month names to numbers in a locale-sensitive fashion. 如果可以提取字符串的相关部分,则仍可以使用传递中的本机Date方法和对象来处理棘手的位,例如以对语言环境敏感的方式将可能缩写的月份名称转换为数字。

This solution, which builds on some comments by Mark Reed, is the one I eventually adopted. 这个解决方案基于Mark Reed的一些评论,是我最终采用的解决方案。

There is no default support for the elided ISO forms (yyyy-mm or yyyy). 省略的ISO格式(yyyy-mm或yyyy)没有默认支持。 The requirement has to use a Date object in order to determine other output fields, eg a day of the week (if a day is specified in the ISO string) such as "Sunday". 需求必须使用Date对象才能确定其他输出字段,例如,星期几(如果在ISO字符串中指定了一天),例如“星期日”。 However, the Date object stores a timestamp, and so missing fields must be defaulted when the elided ISO string is parsed. 但是,Date对象存储一个时间戳,因此在解析消除的ISO字符串时,必须默认缺少字段。 Also, it also needs to use a locale-aware library, such as moment.js. 另外,它还需要使用可识别语言环境的库,例如moment.js。

Although missing fields, such as day or month, would have been defaulted during the parsing, they can be eliminated again at the formatting stage, and a relevant formatting string can be selected based upon the length of the original elided ISO string. 尽管在解析过程中将默认缺少诸如日或月之类的字段,但可以在格式化阶段再次将其消除,并可以根据原始省略的ISO字符串的长度选择相关的格式字符串。

The solution should build upon moment.js by defining new localised formatting strings (similar to L, LL, LLL, etc.) for the elided forms, eg LYM/lym to handle the YYYY-MM ISO case. 该解决方案应在moment.js的基础上,通过为隐藏形式(例如,LYM / lym)定义新的本地化格式字符串(类似于L,LL,LLL等)来处理YYYY-MM ISO情况。 For instance: 例如:

var LY = "YYYY";        // e.g. 2017
var ly = "YY";
var LYM = "MMMM YYYY";      // e.g. December 2017
var lym = "MMM YYYY";
var LYMD = "dddd MMMM D, YYYY";
var lymd = "ddd MMM D, YYYY";   // e.g. Tuesday December 19, 2017

These formatting strings would effectively ignore any defaulted fields that were added when the ISO string was parsed. 这些格式字符串将有效地忽略在解析ISO字符串时添加的任何默认字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM