Moment: 支持十进制毫秒

创建于 2016-06-22  ·  3评论  ·  资料来源: moment/moment

(版本 2.13.0)
ISO 标准允许在日期时间字符串中的“Z”之前有任意数量的十进制秒。 .net 平台似乎使用 7 个小数位 - 而 moment.js 库仅解析出前 3 个小数位。

我有猴子修补格式化程序(toISOString())以从创建对象中提取完整的7位数字 - 但这是一个可怕的黑客。 修补解析器会好得多 - 但超出了我目前的理解。

也许还有另一种解决方法?

最有用的评论

劣质煤,

Ah. I hadn't realised this was a limitation of Javascript.

I had found the option for formatting that you had mentioned - but

对我来说,我真的需要那些额外的数字。 零还不够好。

My 'monkey patch' looks at the creationData for the moment and

手动解析输入字符串中的毫秒数(如果是
可用的)。

//Monkey patch the moment library to use 7 decimal places for seconds in it's standard ISO format - to match C# moment.fn._oldToISOString = moment.fn.toISOString; moment.fn.toISOString = function () { try { var originalDateSplit = moment(this).creationData().input.split("."); var milliseconds = originalDateSplit[originalDateSplit.length - 1]; if (milliseconds.indexOf(this.milliseconds()) === 0) { return moment(this).format('YYYY-MM-DD[T]HH:mm:ss.') + milliseconds + "Z"; } else { return moment(this)._oldToISOString(); } } catch (error) { return moment(this)._oldToISOString(); } }
它并不漂亮 - 并且只适用于那些
最初是从 ISO 日期字符串中解析出来的。 也会不一致
使用毫秒()函数。 一个更好的主意是拦截
解析过程并将任何十进制秒拉入一个单独的属性
moment 对象 - 然后我们可以根据需要返回它。

However - I do appreciate that we are moving beyond the scope of the

在这一点上的 moment.js 库 - 它不再是一个
Javascript 日期对象。

Perhaps we could make this limitation a little clear in the moment.js

文档? 现在毫秒截断是从
示例代码,但没有明确描述 - 并且没有解释为
为什么它会以这种方式行事。

Thank you for your help Maggie - it is much appreciated.

问候,
本·海瑟薇
软件开发人员
DATUM - 岩土和结构监测

所有3条评论

因为 Moment.js 是 JavaScript 中 Date 对象的包装器,所以我们仅限于三位小数(毫秒)。 这是因为日期对象只支持这些,所以真的没有办法存储更多。 不过,您不应该对任何东西进行猴子修补以获得七个小数位来显示或解析。 请注意,实际上只会使用前三个。

moment('2016-01-01T05:21:22.1234567Z').format('YYYY-MM-DDTHH:mm:ss.SSSSSSS')
"2015-12-31T23:21:22.1230000"

请注意,输出是零填充的,但双向都有七位数字。

劣质煤,

Ah. I hadn't realised this was a limitation of Javascript.

I had found the option for formatting that you had mentioned - but

对我来说,我真的需要那些额外的数字。 零还不够好。

My 'monkey patch' looks at the creationData for the moment and

手动解析输入字符串中的毫秒数(如果是
可用的)。

//Monkey patch the moment library to use 7 decimal places for seconds in it's standard ISO format - to match C# moment.fn._oldToISOString = moment.fn.toISOString; moment.fn.toISOString = function () { try { var originalDateSplit = moment(this).creationData().input.split("."); var milliseconds = originalDateSplit[originalDateSplit.length - 1]; if (milliseconds.indexOf(this.milliseconds()) === 0) { return moment(this).format('YYYY-MM-DD[T]HH:mm:ss.') + milliseconds + "Z"; } else { return moment(this)._oldToISOString(); } } catch (error) { return moment(this)._oldToISOString(); } }
它并不漂亮 - 并且只适用于那些
最初是从 ISO 日期字符串中解析出来的。 也会不一致
使用毫秒()函数。 一个更好的主意是拦截
解析过程并将任何十进制秒拉入一个单独的属性
moment 对象 - 然后我们可以根据需要返回它。

However - I do appreciate that we are moving beyond the scope of the

在这一点上的 moment.js 库 - 它不再是一个
Javascript 日期对象。

Perhaps we could make this limitation a little clear in the moment.js

文档? 现在毫秒截断是从
示例代码,但没有明确描述 - 并且没有解释为
为什么它会以这种方式行事。

Thank you for your help Maggie - it is much appreciated.

问候,
本·海瑟薇
软件开发人员
DATUM - 岩土和结构监测

@maggiepint

我正在寻找相同的。 通过moment.js调试后,我注意到它调用了zerofill函数。

由于结果值是一个字符串,我不明白为什么用零填充它🤔🤔🤔
例如,我有这个:

const value = 1596099230963;
const formatString = 'MM/DD/YYYY HH:mm:ss.SSSSSS';
const val = moment.utc(value).format(formatString);
console.log(val);

它打印07/30/2020 08:53:50.963000而不是07/30/2020 08:53:50.963939

此外,我注意到旧版本的moment.js没有做这个零填充,例如 2.5 版。

我现在必须编写一个自定义格式化程序以保留日期中的正确信息,除非对moment.js进行了更改。 我们有机会做出这种改变吗?

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

BonBonSlick picture BonBonSlick  ·  3评论

nikocraft picture nikocraft  ·  3评论

ninigix picture ninigix  ·  3评论

dogukankotan picture dogukankotan  ·  3评论

chitgoks picture chitgoks  ·  3评论