Reputation: 1189
Let's say I have an "events" array which holds "event" objects, like the following:
var events = [
{
name: 'event1',
start: '10.30',
end: '10.45'
},{
name: 'event2',
start: '11.00',
end: '12.00'
}
]
Now, I will have a timeout function doing the following:
setTimeout(function() {
var currentTime = Date.now();
// how can i transform the start and end times from the events object in milliseconds?
var startEventOne = events[0].start // -> how can I transform the string 10.30 in milliseconds?
}, 1000);
The question is, how can I get the time in milliseconds, equivalent to the input string? is it possible?
Upvotes: 0
Views: 119
Reputation: 61
Hej,
var timeArr,
hours,
minutes,
milliseconds,
date;
timeArr = events[0].start.split('.');
hours = timeArr[0];
minutes timeArr[1];
date = new Date(0, 0, 0, hours, minutes, 0, 0);
milliseconds = date.getTime();
// This will return you the number of milliseconds
Upvotes: 1
Reputation: 295
Transform your string in float :
var value = parseFloat(your_string);
Get the entire :
var entire = parseInt(value);
Get the decimal :
var decimal = value - entire;
Convert into ms
Upvotes: 0