9

I have dynamic string with a HH:mm:ss format (e.g. 18:19:02). How can the string be converted into a JavaScript Date object (in Internet Explorer 8, Chrome, and Firefox)?

I tried the following:

   var d = Date.parse("18:19:02");
   document.write(d.getMinutes() + ":" + d.getSeconds());
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 25,389
  • 34
  • 109
  • 165
  • 2
    [RTFM](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse) - you can't create a date if you only have the time. – Blazemonger Dec 10 '12 at 14:10
  • 1
    which year, which month, which day?? – Christoph Dec 10 '12 at 14:12
  • 2
    if date is unimportant but you want to use functions on the Date object, specify an arbitrary date. – Craig Celeste Dec 10 '12 at 14:16
  • Did you get your problem soved? – Christoph Dec 11 '12 at 09:38
  • @Blazemonger As of 2018, you can create a Date object even without a date and time with `new Date()`: see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – alex Nov 20 '18 at 19:45
  • @alex what are you trying to say? `new Date()` has always been valid and takes the clients current date time and time zone as basis. `new Date("18:19:02")` still would be an invalid constructor call under the current spec. – Christoph May 15 '19 at 15:36
  • @Christoph You are correct that `new Date("18:19:02")` is invalid - my comment was that `new Date()` is valid. This creates a Date object without having to specify an exact date (or time). – alex May 15 '19 at 19:43
  • @alex hehe, I got you now. The "as of 2018" confused me a bit, since I though you were suggesting that this is new functionality. Basically you suggest the same I was proposing in my answer? ;-) – Christoph May 23 '19 at 14:01

3 Answers3

24

You cannot create a Date Object directly just from a time like HH:mm:ss.

However - assuming you want the current date(the day portion of the Date object being today) or it doesn't matter for your case - you could do the following:

let d = new Date(); // Creates a Date Object using the clients current time

let [hours, minutes, seconds] = "18:19:02".split(':');

d.setHours(+hours); // Set the hours, using implicit type coercion
d.setMinutes(minutes); // can pass Number or String - doesn't really matter
d.setSeconds(seconds);

// If needed, you could also adjust date and time zone

console.log(d.toString()); //Outputs desired time (+current day/timezone)

Now you have a Date object which contains the time you specified plus the current date and timezone of your client.

Christoph
  • 50,121
  • 21
  • 99
  • 128
8

Try this (without jQuery and a date object (it's only a time)):

var
    pieces = "8:19:02".split(':')
    hour, minute, second;

if(pieces.length === 3) {
    hour = parseInt(pieces[0], 10);
    minute = parseInt(pieces[1], 10);
    second = parseInt(pieces[2], 10);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
silly
  • 7,789
  • 2
  • 24
  • 37
3

The Date object is never properly set because of the missing date part. This should work:

var d = new Date("1970-01-01 18:19:02");
document.write(d.getMinutes() + ":" + d.getSeconds());
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anders H.
  • 25
  • 4