15

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
user1786546
  • 313
  • 2
  • 5
  • 12
  • are the values in your sheet dates or strings ? (ie can you change the date format in the spreadsheet format settings) what was the actual value that returned "12/20/5828963 0:00:00" – Serge insas Mar 05 '13 at 19:44
  • No they are dates, not strings. {3/16/2013 17:00:00 3/16/2013 17:00:00 3/20/2013 5:00:00 3/20/2013 5:00:00 3/21/2013 5:00:00 3/21/2013 5:00:00 3/21/2013 5:00:00 3/25/2013 5:00:00 3/25/2013 5:00:00 3/13/2013 5:00:00 3/26/2013 1:00:01 3/6/2013 4:00:00 3/5/2013 4:00:00 3/19/2013 5:00:00 3/26/2013 1:00:00 3/2/2013 4:00:00 3/18/2013 5:00:00} – user1786546 Mar 05 '13 at 20:22
  • I don't see any separators in your example... ?? see edited answer with an example. – Serge insas Mar 05 '13 at 20:28
  • They're in different cells, so they are separated. I just copy/pasted the cells over. – user1786546 Mar 05 '13 at 20:29

2 Answers2

23

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

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Your example did it. Thanks! For some reason I have to make new Date() a separate variable. – user1786546 Mar 05 '13 at 23:25
  • @user1786546 - of course you have to put it in a separate variable, because the "setHours" function returns a number and if you assign the "new Date" AND "setHours" together, then the "x" variable will get this number and not the date object, as wanted. – TheCuBeMan May 10 '16 at 11:10
-1

When your system time is configured to use UTC:

DateUtils.setSeconds(DateUtils.setMinutes(DateUtils.setHours(new Date(),5),30),0)

Otherwise:

DateUtils.setSeconds(DateUtils.setMinutes(DateUtils.setHours(new Date(),0),0),0)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    please explain or write the result of the action or th esttes you suggest, for us to understand if it answer the question or not – user1767316 May 12 '20 at 15:39