简体   繁体   English

如何在时刻js中将毫秒附加到给定的日期?

[英]How to append milliseconds to the given date in moment js?

Is there any way to format the below date which has HH:MM:SS as time format into HH:MM:SS:MMM in moment js?有什么方法可以将以下具有HH:MM:SS作为时间格式的日期格式化为时刻 js 中的HH:MM:SS:MMM

2016-01-10T00:00:00+05:30

Can it be converted into the format like the below one?可以转换成下图这样的格式吗?

2016-01-10T00:00:00.000+05:30

Simple JS Code:简单的JS代码:

 var pop = moment('2016-01-03'); var pop1 = pop.add(1,'week'); console.log(pop1.format());
 <script src="http://momentjs.com/downloads/moment.js"></script>

You can pass a custom string format argument to the format method in order to customize format output.您可以将自定义字符串格式参数传递给format方法以自定义格式输出。 In your case you can do:在你的情况下,你可以这样做:

pop1.format('YYYY-MM-DDTHH:mm:ss.SSSZ');

where mm means minutes (00..59), ss seconds (00..59) and SSS fractional seconds (000..999).其中mm表示分钟 (00..59)、 ss秒 (00..59) 和SSS小数秒 (000..999)。

The docs says that if you do not pass any arguments to format :文档说,如果您不将任何参数传递给format

As of version 1.5.0 , calling moment#format without a format will default to moment.defaultFormat .从版本1.5.0 开始moment#format没有格式的情况下调用moment#format format 将默认为moment.defaultFormat Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ .开箱即用, moment.defaultFormat是 ISO8601 格式YYYY-MM-DDTHH:mm:ssZ

So if you want to change the way format() (without parameters) works, you can change moment.defaultFormat , for example:所以如果你想改变format() (不带参数)的工作方式,你可以改变moment.defaultFormat ,例如:

moment.defaultFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ';

Here there is a live example:这里有一个活生生的例子:

 var pop = moment('2016-01-03'); var pop1 = pop.add(1,'week'); // By default format() wil give you results in YYYY-MM-DDTHH:mm:ssZ console.log(pop1.format()); // 2016-01-10T00:00:00+05:30 // You can pass a custom format string to the format method console.log(pop1.format('YYYY-MM-DDTHH:mm:ss.SSSZ')); // 2016-01-10T00:00:00.000+05:30 // You can change defaultFormat according your needs moment.defaultFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ'; console.log(pop1.format()); // 2016-01-10T00:00:00.000+05:30
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>

There is certainly a millisecond method which adds milliseconds.当然有一种millisecond方法可以增加毫秒。 See the docs .请参阅文档

You can also see that there is a formatting shortcode for milliseconds - ms in the table here .您还可以看到此处中有一个用于毫秒 - ms的格式化短代码。

String formatting may be of use字符串格式可能有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM