7

I have this code:

v.d = new Date().toLocaleTimeString();

it yields something like this:

20:11:40

So this is the time of day to the second, without milliseconds. My question is: is there a built in call that can show just the time with milliseconds?

I am looking for something like this (to the millisecond):

20:11:40.124
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    *toLocaleTimeString* is implementation dependent, you can't rely on getting any particular format. You might get "8:11:40 pm", or lots of other values. – RobG May 01 '18 at 22:25
  • @RobG I think you should have not marked this question as a duplicate; this question here is very specific about `toLocaleTimeString` which the other Q/A has really nothing related except that both are about JS datetimes. Actually there is a living standard way to get the milliseconds formated using `(new Date).toLocaleTimeString([], { hour12: false, hour: '2-digit', minute: '2-digit', fractionalSecondDigits: 3 })` will return something like `03:40:59.077`. Too pity I can't submit an answer! – Christos Lytras Apr 12 '22 at 00:46
  • @ChristosLytras—there are already 32 answers at the duplicate for formatting time, which is what the OP wants. If you have another answer, post it there. Not specifying a language for *toLocale\** methods means the returned timestamp will be based on the host's language and may not be what you expect. – RobG Apr 12 '22 at 03:56
  • @RobG IMO javascript datetime formating must not be limited to one question and many answers; there are many APIs and libraries to do that and we should not generalize every question about datetime formating. This one here is a very specific question about how to add milliseconds using **built in** method/ways with `toLocaleTimeString` that the other Q/A does not ask, but rather focuses on general datetime formating. IMO, we should not sumup every formating related question to one question and many answers. – Christos Lytras Apr 12 '22 at 13:31

4 Answers4

5

This works, but I am looking for something more compact if possible:

 const d = new Date();
 v.d = d.toLocaleTimeString() + `.${d.getMilliseconds()}`;

this yields:

20:17:30.744

note that to make this work well, you need to add this part: Formatting milliseconds to always 3 digits for d.getMilliseconds() call

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
2

There’s to ISOString(). But stepping back a level, with that exception there’s no standard for formatting dates in js. So you can use toISOString or build up your own string with individual date functions.

Geuis
  • 41,122
  • 56
  • 157
  • 219
2

I did find one issue with your original solution. When I perform it it yields hh:mm:ss PM.mil. I assume you want hh:mm:ss.mil Here is that solution written as a function so you can pass the date object in and get the proper format:

const d = new Date()

const getTimeWithMilliseconds = date => {
  const t = d.toLocaleTimeString();
  return `${t.substring(0,8)}.${date.getMilliseconds() + t.substring(8,11)}`;
}

console.log(getTimeWithMilliseconds(d));

Or if you want it in 24 hour format:

const d = new Date()

const getTimeWithMilliseconds = date => {
  return `${date.toLocaleTimeString('it-US')}.${date.getMilliseconds()}`;
}

console.log(getTimeWithMilliseconds(d));
Cory Kleiser
  • 1,969
  • 2
  • 13
  • 26
1

You can't depend on toLocaleTimeString returning a specific format as it's implementation dependent. It's much more reliable to build the format yourself, e.g.:

function getFormattedTime(date) {
  var d = date || new Date();
  var z = n => ('0'+n).slice(-2);
  var zz = n => ('00'+n).slice(-3);
  return `${z(d.getHours())}:${z(d.getMinutes())}:${z(d.getSeconds())}.${zz(d.getMilliseconds())}`;
}

console.log(getFormattedTime());
console.log(getFormattedTime(new Date(2018,1,1)));
console.log(getFormattedTime(new Date(2018,4,30,23,51,12,89)));

Also see Where can I find documentation on formatting a date in JavaScript?

RobG
  • 142,382
  • 31
  • 172
  • 209