Help with Dates in Apps Script

2,285 views
Skip to first unread message

MenGue

unread,
Feb 24, 2022, 8:46:58 AM2/24/22
to Google Apps Script Community
Hello friends!

I need some help over here. So I have pre-existing data the column A of a spreadsheet this data is exactly a date looking like this (Nov 23, 2021).

I added a Script that runs over column A and compares the existing data with a given date filter anything that is after or equal to Jan 1,  2022, if the dates match to be after or equal to Jan 1, 2022 the script will save it in a new array and then paste it back on the spreadsheet.

Everything is happening as I explained but the dates are being pasted back in this format 11/23/2021 3:00:00 instead of the original way they were Nov 23, 2021 the only thing that I want to get rid of is the hour section 3:00:00

Why is this happening, how can I fix it?

This is the code section block

asinRawData.forEach(function(item,idx){

if(new Date(item[0])>= new Date("Jan 01, 2022")){

  asinFilteredArray.push(item)
   
}

I am more than grateful for any help you may provide.

Regards,

Edward Ulle

unread,
Feb 24, 2022, 9:14:30 AM2/24/22
to Google Apps Script Community
If you want to keep the spreadsheet value as a Date (which I highly recomment) you will need to  Range.setNumberFormats("mmm/d/yyy")

Clark Lind

unread,
Feb 25, 2022, 8:39:42 AM2/25/22
to Google Apps Script Community
As mentioned, if you want to keep the cell object as a date, you will need to format the cell again after pasting the data. If you don't really care, simply change range.getValues() to range.getDisplayValues()
 If your sheet depends on Col A being a date, then you could also just format the date in the function instead of making another call to the sheet:


if(new Date(item[0])>= new Date("Jan 01, 2022")){
//create the date you want:
 let formattedDate = Utilities.formatDate(new Date(item[0]), Session.getScriptTimeZone(), "MMM dd, yyyy");
//replace the first cell of the row with the formatted date:
      item.shift(); 
      item.unshift(formattedDate)

  asinFilteredArray.push(item)
   
}


Gilbert Mendoza

unread,
Feb 25, 2022, 8:31:22 PM2/25/22
to google-apps-sc...@googlegroups.com
Hello Clark!

Thanks for taking the time to review this. I used the getDisplayValues() and it met my needs!

Thank you so much! still learning about JS.

Regards, 

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/jdip05elotI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f14bab81-d623-48f7-91e3-096cd0701f47n%40googlegroups.com.

MenGue

unread,
Feb 25, 2022, 8:53:30 PM2/25/22
to Google Apps Script Community
Thank you so much as well for your inputs. For some reason, I was not able to see this message at first.
Reply all
Reply to author
Forward
0 new messages