momentjs转换格式_使用Moment.js将系统日期转换为ISO格式

I'm trying to convert system date to ISO format in below fashion using momentjs

2015-02-17T19:05:00.000Z

I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried below piece of code..

moment().format("YYYY-MM-DD HH:mm Z");

I get output as 2015-02-02 17:24+05:30.

How can I get it as 2015-02-02T17:24:00.000Z

解决方案

This is pretty well covered in the docs. But, they're long, so here's the specifics:

For some reason, momentjs's definition of ISO 8601 differs from the ECMAScript one, so it isn't built in. The format is YYYY-MM-DDTHH:mm:ss.sssZ and it must be in UTC (the Z denotes this).

So, moment().utc() makes sure the timezone is correct.

Then format it:

moment().utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]");

// 2015-02-02T21:38:04.092Z

The Z is escaped with square brackets. We can do this safely because we forced UTC.

The rest of the characters denote various time elements according to the format table.

You could also do what RobG said and use the native date object. In case you are starting with a moment:

moment().toDate().toISOString( )

// 2015-02-02T21:40:06.395Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值