Mukesh Biswas
Mukesh Biswas

Reputation: 63

How can I get time in millisecond from a given time input?

I am using Jquery Time Picker, Surprisingly, on selecting time it's returning result in format Sun Dec 31 1899 23:00:00 GMT-0600 (Central Standard Time) But, I want to get time in millisecond of selected time (more preciously, selected time on today)?

Upvotes: 2

Views: 787

Answers (1)

Willington Vega
Willington Vega

Reputation: 4872

You can convert the returned value into a Date object with today's year, month and date and the selected time, using the code below:

var today = new Date();
var selectedTimeToday = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate(),
    value.getHours(),
    value.getMinutes(),
    value.getSeconds()
);

If all you want is to get the time in milliseconds for the selected value, then you can use getMilliseconds() or getUTCMilliseconds() to get it:

var timestamp = selectedTimeToday.getMilliseconds();

If you need to calculate the number of milliseconds between now and the selected value, do it like this:

var difference = Math.abs(Date.now() - selectedTimeToday);

Remove Math.abs if you want to use the sign to indicate whether the selected time is in the future (negative difference) or in the past.

Upvotes: 1

Related Questions