Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  34] [ 6]  / answers: 1 / hits: 38969  / 12 Years ago, tue, march 5, 2013, 12:00:00

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.


More From » datetime

 Answers
26

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


[#79809] Tuesday, March 5, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emerald

Total Points: 547
Total Questions: 96
Total Answers: 122

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
;