4

I have a date value like this "2015-09-30T17:32:29.000-05:00" and when i get the getMilliseconds from this date as below , i am just getting 0 and not getting 000. Why? I want to get three digit as milli seconds?

myDate =2015-09-30T17:33:28.000-04:00;
 var msecs = myDate.getMilliseconds() 

i am getting msecs =0. I would like to get msecs as 000. How do i acheive this?

  • 3
    duplicate, or well, answered by http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript what you want is basically padding the string representation of a number with "0" strings. – thomas Sep 30 '15 at 20:29
  • 2
    also here: http://stackoverflow.com/questions/10841773/javascript-format-number-to-day-with-always-3-digits – GrafiCode Sep 30 '15 at 20:29
  • 2
    how do you get that when you are using a Date method on a string? – charlietfl Sep 30 '15 at 20:30
  • @dandavis i am getting as msecs = 0.000. But i just want to get msecs=000 – user5394858 Sep 30 '15 at 20:40
  • @user5394858 - do you really want msecs="000"? – Igor Sep 30 '15 at 20:42
  • 1
    oh, i see. `("000" + n).slice(-3)` – dandavis Sep 30 '15 at 20:42
  • 3
    @dandavis - come on! `if (msecs == 0) return "000";` – Igor Sep 30 '15 at 20:44
  • 1
    Just to add, @GrafiCodeStudio provided the duplicate which is more relevant for this question. The accepted answer for http://stackoverflow.com/questions/10841773/javascript-format-number-to-day-with-always-3-digits is basically the solution. – tcooc Sep 30 '15 at 20:44
  • @Igor: come on where? what if msec=8? i doubt OP wants "8" instead of "008".... the string slice trick will ensure a 3-digit number. i figure the only place you would want extra zeros is to ensure a column width, and in that case, "8" is as bad as "0"... – dandavis Sep 30 '15 at 20:44
  • 2
    @dandavis - I am joking. The OP was only concerned with 0 in his question. – Igor Sep 30 '15 at 20:45
  • 2
    @Igor: it works for that too ;) – dandavis Sep 30 '15 at 20:46

1 Answers1

9

You can pad the number of milliseconds with two zeros (to ensure that there are at least three digits) and then use String.prototype.slice() to get only the last 3 characters (digits).

var msecs = ('00' + myDate.getMilliseconds()).slice(-3);

That way, even if the number of milliseconds returned is already three digits long, the zeros added as padding will be stripped when you slice() the string passing -3 as the argument:

// when the number of milliseconds is 123:
myDate.getMilliseconds() === 123
'00' + myDate.getMilliseconds() === '00123'
('00' + myDate.getMilliseconds()).slice(-3) === '123'

// or, when the number is 0:
myDate.getMilliseconds() === 0
'00' + myDate.getMilliseconds() === '000'
('00' + myDate.getMilliseconds()).slice(-3) === '000'
rhino
  • 13,543
  • 9
  • 37
  • 39
  • what about var msecs = mydate.getMilliseconds().toFixed(2).replace('.', '') – user5394858 Sep 30 '15 at 20:47
  • Is there anything by using toFixed() method here? – user5394858 Sep 30 '15 at 20:48
  • 1
    forget about `toFixed()` - that's for decimals and you're working with integers. – dandavis Sep 30 '15 at 20:48
  • Using `toFixed()` that way, you would end up with the number 3 (just an example) becoming '300'. – rhino Sep 30 '15 at 20:49
  • i would like to go for if (msecs == 0) return "000"; – user5394858 Sep 30 '15 at 20:52
  • If that suits your needs, then go for it. But keep in mind that it would only work for 0, i.e. you wouldn't get a 3-digit padded number out of numbers that are 1- or 2-digit long. – rhino Sep 30 '15 at 20:53
  • @rhino i may get as you have menionted. what is the best approach you want me to use? Kindly advise me. – user5394858 Sep 30 '15 at 21:10
  • @user5394858 There is no approach that I *want* you to use. I am simply presenting you with an optimal approach that takes into account not only the case when the number of milliseconds is 0 (1 digit), but also when it is a two- or three-digit number, and turns it into a three-digit number either way (`0` becomes `000`, `5` becomes `005`, `12` becomes `012`, `456` remains unchanged). If you **only** want to turn `0` into `000` and keep other numbers unaffected, then `if (msecs === 0) return '000';` is perfectly fine. – rhino Sep 30 '15 at 22:05