Dany D
Dany D

Reputation: 1189

Get the time in milliseconds, corresponding to a custom string

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

Answers (2)

Glenn
Glenn

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

Jules
Jules

Reputation: 295

  1. Transform your string in float :

    var value = parseFloat(your_string);
    
  2. Get the entire :

     var entire = parseInt(value);
    
  3. Get the decimal :

    var decimal = value - entire;
    
  4. Convert into ms

Upvotes: 0

Related Questions