momentjs内部对象是什么"_d"?与"_i" [英] momentjs internal object what is "_d" vs "_i"

查看:498
本文介绍了momentjs内部对象是什么"_d"?与"_i"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用momentjs,并使用moment.hour(xx),moment.minute(xx)处理日期.

I am using momentjs and manipulating a date using moment.hour(xx) moment.minute(xx).

当我在console.log中看到该对象包含_d和_i时: _d包含正确更改的moment.hour()或moment.minute()更改,但是_i对象包含原始更改?

When i console.log the moment i see that the object contains a _d and _i: the _d contains the correct changed moment.hour() or moment.minute() changes however the _i object contains the original?

k {_isAMomentObject: true, _i: Thu Dec 11 2014 20:34:00 GMT+0200 (South Africa Standard Time), _isUTC: false, _pf: Object, _locale: j…}
_d: Thu Dec 11 2014 14:00:00 GMT+0200
_i: Thu Dec 11 2014 20:34:00 GMT+0200

有人可以启发我吗?

推荐答案

请不要理会这些.请使用各种输出功能,例如.format(). 请参阅有关此主题的Moment.js指南.

Pay no attention to those. Use the various output functions, such as .format() instead. See the Moment.js guidance on this topic.

由于Date对象的工作原理,此刻内部有一些古怪之处.公共API中的所有功能都将它们考虑在内,但是您可能不想自己弄清楚它们.

The moment internals have some quirks due to how the Date object works. All of the functions in the public API take them into account, but you probably don't want to figure them out yourself.

不过,为了完整起见,我将详细说明它们的目的:

Just to be complete though, I'll elaborate on their purpose:

  • _i是创建moment对象时使用的输入.它可以是字符串,数字,数组或Date对象.

  • _i is the input used when create the moment object. It can be a string, a number, an array, or a Date object.

但是,如果传入另一个moment对象,则_i将被复制到_i的那一刻,其他属性也将被复制. _i永远不会是moment对象.

However, if another moment object is passed in, the _i will be copied to that moments _i, and other properties will also be copied over. _i will never be a moment object.

_i也可以是未定义的.

_d是支持moment对象的Date对象的实例.

_d is the instance of the Date object that backs the moment object.

如果您处于本地模式",则_d将具有与公共API展示对象的时刻相同的本地日期和时间. getTimevalueOf返回的时间戳也将匹配.

If you are in "local mode", then _d will have the same local date and time as the moment object exhibits with the public API. The timestamps returned by getTime or valueOf will also match.

如果您处于"UTC模式",则_d仍将具有与对象通过公共API展示的时间相同的 UTC 日期和时间.这可能会造成混淆,因为您需要查看getUTCDate_d上的其他基于UTC的功能,以查看它们是否匹配.时间戳也将在此处匹配.

If you are in "UTC mode", then _d will still have the same UTC date and time as the moment object exhibits with the public API. This may be confusing, as you'd need to look at getUTCDate and other UTC-based functions on _d in order to see them match. The timestamps will still match here as well.

如果使用utcOffsetzonetz函数更改了时区偏移,则_d不能单独使用.它还必须考虑是否定义了_offset.如果是,那么必须首先通过偏移量来调整支持_d对象的时间戳.您可以在valueOf方法的实现中看到此行为此处.

If you've changed the time zone offset, with the utcOffset, zone, or tz functions, then the _d value cannot stand alone. It must also consider if _offset is defined. If it is, then the timestamp backing the _d object has to first be adjusted by the amount of the offset. You can see this behavior in the implementation of the valueOf method here.

这是当前版本中这两个字段的行为(在我撰写本文时为2.10.6).但是,还有其他字段,并且由于这些字段是 internal 字段,因此在将来的版本中行为很可能会发生变化.特别是,请参见 issue#2616 .

This is the behavior for these two fields as of the current version (2.10.6 as I'm writing this). However, there are other fields as well, and since these are internal fields, it's entirely possible the behavior could change in a future version. In particular, see issue #2616.

这篇关于momentjs内部对象是什么"_d"?与"_i"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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