Formatting a date in javascript till the millisecond
-
27-04-2021 - |
سؤال
We are using the following js lib from Microsoft https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js
var datetimehigh = new Date(2011,01,12,14,45,55,596);
var sDate = datetimehigh.format("dd/MM/yyyy HH:mm:ss sss");
I cannot get the millisecond part to work.Note that format comes from Microsoft's Mvc Ajax lib.
المحلول
It's indicated by an f:
"dd/MM/yyyy HH:mm:ss fff"
نصائح أخرى
If you are using the native Date
javascript object, you can simply use .toISOString
method to get a formatted string with milliseconds:
const date = new Date();
const dateString = date.toISOString(); // "2020-01-06T19:57:12.146Z"
Note that using .toString
won't give you milliseconds precision.
Using the date format library, it should be something like this:
var nowMilliseconds = new Date().format("yyyy-mm-dd HH:MM:ss l");
http://blog.stevenlevithan.com/archives/date-time-format
L
for milliseconds with two digits
l
(minus) for milliseconds with three digits
Use 'S' for milliseconds formatting:
"dd/MM/yyyy HH:mm:ss:SSS"
لا تنتمي إلى StackOverflow