user1786546 user1786546 - 1 month ago 7
Javascript Question

Set Date/Time to 00:00:00 in Google Apps Script

I have an array of dates with different times in the date.

Example: {4/15/13 05:00:00, 03/10/13 13:00:00, 02/10/13 02:00:00, etc.}

If I wanted to change just the hour to 00:00:00 so the array would read:

{4/15/13 00:00:00, 03/10/13 00:00:00, 02/10/13 00:00:00, etc.}

How would I do that?

I've tried using getTime(), but I don't have control over just the hours. Any help would be great.
Thanks!

EDIT: I tried this loop:

for (var i = 0; i < gValues.length; i++) { // repeat loop
sheet.getRange(i + 2, 7, 1, 1).setValue(gValues[i][0].setHours(00,00,00,00));
}


But instead of the desired result, I get this value: "12/20/5828963 0:00:00" for every single cell.

Answer

Google Apps Script is JavaScript, date manipulations are explained here,

setting the hours goes like this :

Date.setHours(hour,min,sec,millisec)

here is an example :

//   3/16/2013 17:00:00

function test(){
 var x = new Date('3/16/2013 17:00:00');// x is now a date object
 x.setHours(0,0,0,0); set  hours to 0, min Secs and milliSecs as well
 Logger.log(x);// show result
}

Logger value : Sat Mar 16 00:00:00 GMT+01:00 2013

Comments