Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a date into an integer?

I have an array of dates and have been using the map function to iterate through it, but I can't figure out the JavaScript code for converting them into integers.

This is the array of dates:

var dates_as_int = [     "2016-07-19T20:23:01.804Z",     "2016-07-20T15:43:54.776Z",     "2016-07-22T14:53:38.634Z",     "2016-07-25T14:39:34.527Z" ]; 
like image 242
Pablo.K Avatar asked Aug 01 '16 14:08

Pablo.K


People also ask

How do I convert a date to an integer in java?

Or, if you really want to convert the 'date' into integer type 06/03/2017 to 06032017 .. you can do something like this. SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy"); System. out. println(Integer.

How do I convert a date object to number?

Example: Convert Date to Number 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.

Would date be an integer?

A calendar date is stored internally as an integer value equal to the number of days since December 31, 1899. Because DATE values are stored as integers, you can use them in arithmetic expressions. For example, you can subtract a DATE value from another DATE value.


2 Answers

var dates = dates_as_int.map(function(dateStr) {     return new Date(dateStr).getTime(); }); 

=>

[1468959781804, 1469029434776, 1469199218634, 1469457574527] 

Update: ES6 version:

const dates = dates_as_int.map(date => new Date(date).getTime()) 
like image 134
Alex Bass Avatar answered Sep 25 '22 19:09

Alex Bass


Using the builtin Date.parse function which accepts input in ISO8601 format and directly returns the desired integer return value:

var dates_as_int = dates.map(Date.parse); 
like image 32
Alnitak Avatar answered Sep 25 '22 19:09

Alnitak