如何使用MomentJS转换为2016-10-19T08:00:00 Z
在一个项目中,我们使用的是与日期使用的MomentJ。从后端,我们成为以下格式的日期:2016-10-19T08:00:00 Z(不要问我为什么...) 现在,我们正在从某些Selectbox中设置一个新日期。而且我正在尝试以相同的格式转换它:
const date = '25.03.2021';
const hour = '13';
const minute = '45'; // this 3 values come from value of selectboxes
const rawDate = moment(date).hour(hour).minute(minute);
// trying to convert to 2021-03-25T13:45:00Z
rawDate.format(); // output: 2021-03-25T13:45:00+00:00
rawDate.format('DD.MM.YYYY hh:mm:ss'); // output: 03.01.2022 08:00:00
rawDate.format('DD.MM.YYYY hh:mm:ss z'); // output: 03.01.2022 08:00:00 UTC
rawDate.format('DD.MM.YYYY hh:mm:ss Z'); // output: 03.01.2022 08:00:00 +00:00
rawDate.toISOString(); // output: 2022-01-03T08:00:00.000Z
我知道我可能只能使用格式()或toisostring()并切成/替换最后一个位。但是我想知道有没有任何弦乐/操纵的方法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 moment.utc()确保您的日期在UTC中,然后使用。
我还建议将您从此刻()呼叫中解析的格式定义的格式,例如通过“ dd.mm.yyyy”作为第二个论点。
后端以这种格式进行日期的原因是,这是一种使其建立机器可读且一致的格式化日期的标准化方法( ISO 8601 )
You could use moment.utc() to ensure your date is in UTC, then use .format() with the format string
DD-MM-YYYYTHH:mm:ss[Z]
.I'd also suggest explicity defining the format you are parsing from in the moment() call, e.g. pass 'DD.MM.YYYY' as the second argument.
The reason the backend takes dates in this format is that it's a standardized way of formatting dates to make them machine-readable and consistent (ISO 8601)
您可以尝试转换为UTC ..?
即您打算使用UTC日期/时间..?
You can try convert to UTC ..?
i.e. Do you intend to make use of a UTC date/time..?