10

This is what I do in Ruby.

time = Time.now
=> 2013-10-08 12:32:50 +0530
time.to_i //converts time to integer
=> 1381215770
Time.at(time.to_i) //converts integer to time
=> 2013-10-08 12:32:50 +0530

I'm trying to implement the same with Node.js, but not sure how to do it. Kindly help me in finding a module for implementing the same with Node.js, Javascript. Thanks!

3 Answers 3

22

In javascript world.

Date.now()

and

new Date(1381216317325);
Sign up to request clarification or add additional context in comments.

2 Comments

It should be noted, that in Javascript Date is stored as the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch) (usually only seconds - in Ruby) and that Date.now() returns only actual time, if you need to convert any Date object to integer, use Date.getTime().
@ivoszz Date().getTime() NOT Date.getTime()!
6

In addition to user10 answer

Date.parse("2013-10-08 12:32:50 +0530");

will get you time as integer

EDIT
Date API

1 Comment

I really need this. Thanks!
2
new Date().getTime(); 

will return an integer which represent the time in milliseconds spent since midnight 01 January, 1970 UTC. This need to be parsed somehow to be more human readable.

There is no default method implemented in Javascript which translate this number to a human interpretable date, so you have to write yourself.

A simple method would be this:

function getTime() {
    var now = new Date();
    return ((now.getMonth() + 1) + '-' +
            (now.getDate()) + '-' +
             now.getFullYear() + " " +
             now.getHours() + '-' +
             ((now.getMinutes() < 10)
                 ? ("0" + now.getMinutes())
                 : (now.getMinutes())) + ':' +
             ((now.getSeconds() < 10)
                 ? ("0" + now.getSeconds())
                 : (now.getSeconds())));
}

console.log(getTime());

You can adjust yourself the order of appearance.

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.