Subtracting 10 hours should move to yesterday, but it doesn't

29 Views Asked by At

I'm subtracting 10 hours from date object. Expecting to get yesterday's date.

But I get the same date. Is it a bug in ads-script?

enter image description here

  var now = new Date();
  Logger.log("now.getHours() = "+now.getHours());
  var nowDateStr = Utilities.formatDate(now, AdsApp.currentAccount().getTimeZone(),  'yyyy-MM-dd');
  Logger.log("nowDateStr = "+nowDateStr);
  
  var past = new Date(now.getTime() - 10 * 3600 * 1000);
  var pastHour = past.getHours();

  var pastDateStr = Utilities.formatDate(past, AdsApp.currentAccount().getTimeZone(),  'yyyy-MM-dd');
  query = "SELECT customer.id, metrics.impressions, segments.hour FROM customer WHERE metrics.impressions = 0 AND segments.hour = " + pastHour + " AND segments.date = '" + pastDateStr + "'";
  Logger.log("query " + query);
1

There are 1 best solutions below

0
On

A timezone issue. past.getHours() returns the hour in 'local' time meaning the time zone of the machine running the query, probably UTC or PDT vs pastDateStr is computed with the timezone of the Ads account.

var pastHourStr = Utilities.formatDate(past, AdsApp.currentAccount().getTimeZone(), 'H');