Reputation:
given an array of dates and times in Eastern Standard Time like...
var dates = [
new Date(2015, 6, 15, 14, 00, 00),
new Date(2015, 6, 15, 18, 30, 30),
new Date(2015, 7, 30, 0, 15, 52),
new Date(2015, 9, 8, 10, 08, 38)];
How would you convert these upcoming dates to UTC in milliseconds since 1-1-1970 and save those values in a new array?
Thanks for any help.
Upvotes: 1
Views: 955
Reputation: 2559
Iterrate the array, use date.getTime()
to get the seconds from 1-1-1970 and than push it into an new array.
Fiddle: http://jsfiddle.net/h22f5rw8/
Edit
The code if JSFiddle going down or isn't reachable:
var dates = [
new Date(2015, 6, 15, 14, 00, 00),
new Date(2015, 6, 15, 18, 30, 30),
new Date(2015, 7, 30, 0, 15, 52),
new Date(2015, 9, 8, 10, 08, 38)
];
var dates_ms = [];
dates.forEach( function(datum) {
dates_ms.push(datum.getTime());
});
console.log( dates_ms );
output: [1436961600000, 1436977830000, 1440886552000, 1444291718000]
Upvotes: 1
Reputation: 493
Just use the subtraction operator.
var oldDate = new Date(1970, 0, 1);
var newDates = []
for(var i = 0; i < dates.length; i++) {
newDates.push(dates[i] - oldDate);
}
EDIT: Just realized you wanted to modify the original array EDIT: Misread the question
Upvotes: 1
Reputation: 16847
You could do something like this
var timestamps = dates.map(function(d){return +d});
Upvotes: 0
Reputation: 1995
Use Date.getTime()
function of Javascript
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
Upvotes: 1