最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get date time in specific format in moment js - Stack Overflow

programmeradmin0浏览0评论

I am trying to get the date time in moment js in this format :

2016-12-19T09:43:45.672Z

The problem is I am able to get the time format as

2016-12-19T15:04:09+05:30

using

moment().format();

but I need the format as the first one [like .672Z instead of +05:30]. Any suggestions would be of great help.

I am trying to get the date time in moment js in this format :

2016-12-19T09:43:45.672Z

The problem is I am able to get the time format as

2016-12-19T15:04:09+05:30

using

moment().format();

but I need the format as the first one [like .672Z instead of +05:30]. Any suggestions would be of great help.

Share Improve this question edited Dec 19, 2016 at 9:53 Neha Bajpai asked Dec 19, 2016 at 9:51 Neha BajpaiNeha Bajpai 1672 gold badges4 silver badges13 bronze badges 1
  • Probably you can simply use moment's toISOString() method. – VincenzoC Commented Dec 19, 2016 at 9:58
Add a ment  | 

4 Answers 4

Reset to default 6

From the documentation on the format method:

To escape characters in format strings, you can wrap the characters in square brackets.

Since "Z" is a format token for the timezone, you need to escape it. So the format string you are after is:

moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');

As @VincenzoC said in a ment, the toISOString() method would also give you the format you are after.

var dateTime = new Date("2015-06-17 14:24:36");
dateTime = moment(dateTime).format("YYYY-MM-DD HH:mm:ss");

Try this. You should have the date in the correct format.

Use moment.utc() to display time in UTC time instead of local time:

var dateValue = moment().utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';

or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):

var dateValue = moment().toISOString();

Try this

    const start_date = '2018-09-30';
    const t = moment(start_date).utc().format();
    console.log(t);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.2/moment.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论