Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Script- Convert Date to Epoch Time

I've been working on this for a long time and haven't quite found anything that fits what I need.

Right now, I have an array of dates- all of them look like this -

Sun Mar 31 2013 00:00:00 GMT+0700 (ICT).

I'm looking for a way to convert that into

1364688000

Only through Google Script.

How would you go about doing this?

like image 603
Iwill Bomphlanh this Avatar asked Sep 03 '14 09:09

Iwill Bomphlanh this


2 Answers

=(A3-DATE(1970,1,1))*86400

With A3 being something like "2/20/2018"

like image 180
Kyle Pennell Avatar answered Sep 29 '22 07:09

Kyle Pennell


The getTime() method applied to a date returns the number of milliseconds since the epoch reference in JavaScript, ie what you are looking for.

Logger.log(new Date(2013,3,31,0,0,0,0).getTime());

Your value is in seconds, so we can divide /1000

but the value you are showing is not correct, result has an offset of 31 days in GMT 0 ... How are you getting the value 1364688000 ?

test code (script properties in UTC (GMT 0 without daylight savings)

function timeInMsSinceOrigin(){
  var val = new Date(2013,3,31,0,0,0,0).getTime()/1000;// in seconds
  var offset = val-1364688000; //in seconds too
  Logger.log('value = '+val+ '   offset : '+offset/3600+' hours  ='+offset/(3600*24)+' days');
}

Logger result : value = 1367366400 offset : 744 hours =31 days

like image 41
Serge insas Avatar answered Sep 29 '22 08:09

Serge insas