Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a date in javascript till the millisecond

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.

like image 240
chugh97 Avatar asked Feb 09 '12 14:02

chugh97


People also ask

How do you write milliseconds in JavaScript?

getUTCMilliseconds() returns the milliseconds (0 to 999) of a date. getUTCMilliseconds() returns the milliseconds according to UTC.

How do you go from Date to milliseconds?

Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.

How do I format a Date in JavaScript?

const d = new Date("2015/03/25"); The behavior of "DD-MM-YYYY" is also undefined. Some browsers will try to guess the format. Some will return NaN.

How do you format milliseconds?

Usually we display time in in 12 hour format hh:mm:aa format (e.g. 12:30 PM) or 24 hour format HH:mm (e.g. 13:30), however sometimes we also want to show the milliseconds in the time. To show the milliseconds in the time we include “SSS” in the pattern which displays the Milliseconds.


2 Answers

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.

like image 138
Martín De la Fuente Avatar answered Oct 12 '22 04:10

Martín De la Fuente


It's indicated by an f:

"dd/MM/yyyy HH:mm:ss fff"
like image 23
inhan Avatar answered Oct 12 '22 05:10

inhan