Date Time Error

200 views
Skip to first unread message

Grace C

unread,
Jul 20, 2023, 2:26:04 PM7/20/23
to Google Ads Scripts Forum
Hi there,

I'm trying to pull out the date / time using the functions here but my script is not returning any values. Would appreciate any help on this. 


Thanks,
Grace

 //Retrieves sitelinks from account
 
function main() {
 
  var now = new Date();
  var timeZone = AdsApp.currentAccount().getTimeZone();
  var datestring = Utilities.formatDate(now, timeZone, 'yyyyMMdd');
  var today = new Date(datestring);
   
  Logger.log("today date is " + today)
 
  //iterates through our ad groups
  var campaignIter = AdsApp.campaigns()
  .withCondition("metrics.impressions > 10")
  .withCondition("campaign.status = ENABLED")
  .forDateRange("LAST_MONTH")
  .get();
 
  while(campaignIter.hasNext()){  
    var campaign = campaignIter.next();
  Logger.log(campaign)
   
  //finds campaigns' corresponding sitelinks
    var SitelinkSelector = campaign.extensions().sitelinks()
    var SitelinkIterator = SitelinkSelector.get();
    while(SitelinkIterator.hasNext()){
    var campaignSitelink = SitelinkIterator.next();
     
      campaignSitelink.getEndDate()
      var sitelinkenddate = campaignSitelink.getEndDate()
           
     if (sitelinkenddate != null) {
       
      var sitelinkYear = sitelinkenddate.year.toString();
      var sitelinkMonth = (sitelinkenddate.month<10) ? "0" + sitelinkenddate.month : sitelinkenddate.month.toString();
      var sitelinkDay = (sitelinkenddate.day<10) ? "0" + sitelinkenddate.day : sitelinkenddate.day.toString();
      var specdate = sitelinkYear + sitelinkMonth + sitelinkDay;
      Logger.log(specdate);

       
     if (today > specdate ) {  
      Logger.log(specdate)
       campaign.removeSitelink(campaignSitelink)

    }

  }
}
}}

Google Ads Scripts Forum Advisor

unread,
Jul 20, 2023, 7:43:35 PM7/20/23
to adwords...@googlegroups.com

Hi Grace,

Welcome to the Google Ads Scripts forum channel.

Upon checking, it appears that the "yyyyMMdd" format produces an invalid date. I suggest you check this link (https://developers.google.com/google-ads/scripts/docs/features/dates) to learn more on how to work with dates and times in Google Ads Scripts. Based on the "Creating a date object from a formatted date string" section of the link I provided, the date constructor can only parse certain date string formats. To make sure your date string is parsed correctly, always provide it in the MMMM dd, yyyy HH:mm:ss Z format.

This message is in relation to case "ref:_00D1U1174p._5004Q2nA2MB:ref"

Thanks,
 
Google Logo Google Ads Scripts Team


Grace C

unread,
Jul 23, 2023, 9:50:52 PM7/23/23
to Google Ads Scripts Forum
Hi,

Thanks for the response. I'm trying to run a bulk upload sitelink script which requires me to pass back the date time in 8 digit format here: https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_sitelinkbuilder

Would you be able to help recommend a workaround based on my script then if the new experience doesnt take in the 8 digit datetime format?

Thanks
Grace

Google Ads Scripts Forum Advisor

unread,
Jul 24, 2023, 4:35:48 AM7/24/23
to adwords...@googlegroups.com

Hi Grace,

 

Thanks for getting back.

 

Upon further investigation, it appears that the issue is due to having created a date object, passing that within the format, and then passing those again into a new date object. I would recommend going through the documentation <https://developers.google.com/google-ads/scripts/docs/features/dates#basic_concepts> our team previously provided as it also shows an example on creating a date object and logging it formatted. I would suggest just passing the formatted date object to a variable and logging that without passing it to a new date object. You could refer to the snippet of code below which should output to a yyyyMMdd format:

var now = new Date();
var timeZone = AdsApp.currentAccount().getTimeZone();
var dateString = Utilities.formatDate(now, timeZone, 'yyyyMMdd');
   
Logger.log("Today's date is " + dateString); // yyyyMMdd format

 

If this is not what you're looking for, kindly provide the following information and elaborate on your use-case so we're able to further investigate and guide you accordingly:

  • Google Ads account ID
  • Name of the script

Grace C

unread,
Jul 24, 2023, 6:18:17 AM7/24/23
to Google Ads Scripts Forum
Thank you for the quick reply. 

That works. However im now encountering a new error - The JavaScript runtime exited unexpectedly.

Are you able to help take a look in the following account please? That'll be much appreciated.
- Google Ads account ID:  246-504-4867
- Name of the script: Sitelink Builder | #3 | Remove Expired Sitelinks (Campaign)
Reply all
Reply to author
Forward
0 new messages