37

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.

chugh97
  • 9,602
  • 25
  • 89
  • 136

5 Answers5

38

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.

Martín De la Fuente
  • 6,155
  • 4
  • 27
  • 28
  • 2
    Also note that `toISOString()` will change the time to UTC. The timezone offset can be removed before formatting with `date.setMinutes(date.getMinutes() - date.getTimezoneOffset())` – DustInComp Mar 28 '22 at 12:07
24

It's indicated by an f:

"dd/MM/yyyy HH:mm:ss fff"
inhan
  • 7,394
  • 2
  • 24
  • 35
  • 5
    Its not working as expected: myDateTime.format("mm-dd-yy HH:MM:ss fff") gives: "12-03-18 11:28:49 fff" – AleX_ Dec 03 '18 at 17:35
13

Use 'S' for milliseconds formatting:

"dd/MM/yyyy HH:mm:ss:SSS"
KayV
  • 12,987
  • 11
  • 98
  • 148
8

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

Yako
  • 3,405
  • 9
  • 41
  • 71
  • 2
    Thanks, just what I was looking for. This is great for use in Node.js with the ported dateformat module: https://www.npmjs.com/package/dateformat – Josh1billion Mar 02 '16 at 17:16
  • Great! Works as expected: myDateTime.format("mm-dd-yy HH:MM:ss.l") "12-03-18 11:28:49.000" – AleX_ Dec 03 '18 at 17:36
  • 2
    Having trouble with `new Date()`: _(intermediate value).format is not a function"_ – WestCoastProjects Jun 20 '20 at 17:33
  • @StephenBoesch it's prototype used to be provided by https://www.npmjs.com/package/dateformat, but is being deprecated. You could still use the library tho. – Nikola Petkanski Mar 29 '21 at 14:41
0

Here's how I do it:

function date_to_string_with_milliseconds(date){
  let date_str = date.toString() 
  let date_without_milliseconds = new Date(date_str) // truncated date since milliseconds are not included
  let milliseconds_delta = date - date_without_milliseconds
  let date_str_with_milliseconds = date_str.replace(/(^.*:\d\d:\d\d)(.*$)/, `$1:${milliseconds_delta}$2`)
  return date_str_with_milliseconds
}

Usage:

date_to_string_with_milliseconds(new Date(Date.now())).toString()
// outputs 'Wed Nov 30 2022 16:40:42:991 GMT+0530 (India Standard Time)'

Doing it this way has a couple of advantages:

  1. The date string that you get as output can be converted back to a Date object.

  2. This doesn't change the original date object in any way since you only need a string representation.

Capstone
  • 2,254
  • 2
  • 20
  • 39