0

I m working on a scheduled TV on a raspberry pi (raspbian) in javascript/node.js

I extract the information of what to play in a .smil file, and one of the field is scheduled, wich is a string of the format "YYYY-MM-DD hh:mm:ss"

To make comparison between them and the system time, I would like to convert it in UNIX timestamp.

Is there a better way than a function like this one:

function (stime){
    var time=0, cache=0, scache;

    scache=stime.substr(0, 4);
    cache=parseInt(scache);
    time=cache*365*24*60*60;

    ...

    return time;
}

And so on for mounth, day...?

1 Answer 1

7

May be this can help

var date = new Date('2009-07-15 00:00:00'.split(' ').join('T'))

that will give you date object and to get timestamp from it you can do

date.getTime() / 1000

dividing by 1000 because getTime will give timestamps in milliseconds

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

2 Comments

I don't think it returns it as milliseconds anymore
For such date: "2016-05-10T19:27:21.338Z" method .getTime()/1000 delivers 1462908441.338. I propose the following snippet for such case: ~~(date.getTime() / 1000)

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.