Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Convert Time to Integer and Integer to Time

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!

like image 477
Srikanth Jeeva Avatar asked Oct 08 '13 07:10

Srikanth Jeeva


People also ask

How to convert time to integer in js?

A simple method would be this: function getTime() { var now = new Date(); return ((now. getMonth() + 1) + '-' + (now.

How to convert datetime to number in JavaScript?

The new Date() gives the current date and time. To convert the name to a number, we use the getTime() method. The getTime() method returns the number of milliseconds from midnight of January 1, 1970 (EcmaScript epoch) to the specified date.

How can I convert a Date into an integer?

strftime() object. In this method, we are using strftime() function of datetime class which converts it into the string which can be converted to an integer using the int() function. Returns : It returns the string representation of the date or time object.

How do I convert time to string in numbers?

parseInt(timef[1]); int second=Integer. parseInt(timef[2]); int temp; temp = second + (60 * minute) + (3600 * hour); System. out. println("seconds " + temp);


2 Answers

In javascript world.

Date.now()

and

new Date(1381216317325);
like image 78
user10 Avatar answered Nov 03 '22 12:11

user10


In addition to user10 answer

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

will get you time as integer

EDIT
Date API

like image 39
Dmitry Matveev Avatar answered Nov 03 '22 11:11

Dmitry Matveev