15

I have a string that looks like "01:12:33" which is HH:MM:SS format. How can I convert that to a time value in JS?

I've tried the new Date() constructor and setting the year and day values to 0, then doing getTime(), but I am not having any lucky.

Milos
  • 1,073
  • 4
  • 14
  • 33
  • You want a date object, or a unix timestamp? – Ethan Oct 05 '17 at 17:11
  • date object, something that looks like HH:MM:SS preferably – Milos Oct 05 '17 at 17:12
  • 1
    Unfortunately, JS does not have a class to represent a date without time. See this other post for viable alternatives: https://stackoverflow.com/questions/17884586/javascript-parsing-times-without-date – Haroldo_OK Oct 05 '17 at 17:19
  • 1
    Possible duplicate of [Javascript parsing Times without Date](https://stackoverflow.com/questions/17884586/javascript-parsing-times-without-date) – Haroldo_OK Oct 05 '17 at 17:20

3 Answers3

27

Prefix it with a date:

var hms = "01:12:33";
var target = new Date("1970-01-01T" + hms);
console.log(target);

There target.getTime() will give you the number of milliseconds since the start of the day;

Or, if you need it to be today's date:

var now = new Date();
var nowDateTime = now.toISOString();
var nowDate = nowDateTime.split('T')[0];
var hms = '01:12:33';
var target = new Date(nowDate + 'T' + hms);
console.log(target);

There target.getTime() will give you the number of milliseconds since the epoch.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 1
    for todays date I needed to add a space after nowDate to correctly format var target = new Date(nowDate + " " + hms); – Nick R Aug 22 '22 at 08:23
  • "1970-01-01 01:12:33" is not a format supported by ECMA-262, so parsing is implementation dependent. Safari at least returns an invalid date. – RobG Oct 04 '22 at 05:50
4

You can add the following function that does the job for you :

function getDateFromHours(time) {
    time = time.split(':');
    let now = new Date();
    return new Date(now.getFullYear(), now.getMonth(), now.getDate(), ...time);
}
console.log(getDateFromHours('01:12:33'));
M0nst3R
  • 5,186
  • 1
  • 23
  • 36
2

To be able to do this, there should be a conversion of the string in HH:MM:SS format to JavaScript time.

Firstly, we can use Regular Expression (RegEx) to properly extract the values in that string.

let timeString = "01:12:33";

Extract values with RegEx

let regExTime = /([0-9]?[0-9]):([0-9][0-9]):([0-9][0-9])/;
let regExTimeArr = regExTime.exec(timeString); // ["01:12:33", "01", "12", "33", index: 0, input: "01:12:33", groups: undefined]

Convert HH, MM and SS to milliseconds

let timeHr = regExTimeArr[1] * 3600 * 1000;
let timeMin = regExTimeArr[2] * 60 * 1000;
let timeSec = regExTimeArr[3] * 1000;

let timeMs = timeHr + timeMin + timeSec; //4353000 -- this is the time in milliseconds.

In relation to another point in time, a reference time has to be given.

For instance,

let refTimeMs = 1577833200000  //Wed, 1st January 2020, 00:00:00; 

The value above is is the number of milliseconds that has elapsed since the epoch time (Jan 1, 1970 00:00:00)

let time = new Date (refTimeMs + timeMs); //Wed Jan 01 2020 01:12:33 GMT+0100 (West Africa Standard Time)
Foz
  • 88
  • 1
  • 6
  • Good job! Could you explain the question mark in between bracket? – Toma Jun 22 '21 at 23:54
  • 1
    It means the entity before it (to the left) could be present or not. That is, 1 or 0 instance of that entity. Important if you want your hours to read single digits without a leading '0'. That is, '1', instead of '01'. – Foz Sep 20 '21 at 09:55
  • why does it add one hour? – Roman Sterlin Nov 03 '21 at 12:31