0

This question answers how to convert HH:mm:ss string into a Javascript date object. The string returned from HTML time input is not always in the HH:mm:ss format. The format varies.

The answers in the linked question will not work in-case of dynamic formats.

How to create a Javascript date object from this input value which does not have a fixed format?

3

3 Answers 3

13

This is an optimised version of benihamalu's answer.

const today = new Date();
console.log(new Date(today.toDateString() + ' ' + "13:30"));

Sign up to request clarification or add additional context in comments.

Comments

2

I assume you want current date, in that case you need to get the current date and then pass the current time.

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();

today = mm + '/' + dd + '/' + yyyy;
console.log(new Date(today + " " + "13:30" /*pass your time*/));

Comments

1

simple answer

function TimeToDate(time) {
    var today = new Date();
    time = new Date('1970-01-01' + ' ' + time + 'Z').getTime();
    var date = today.setHours(0, 0, 0, 0);
    var DateTime = new Date(date + time);
    return DateTime;
}

console.log(TimeToDate("13:30:7.026"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.