I have a spreadsheet which we use to log our work. I have read the date’s column from the sheet into an array so that I can perform actions/calculations on them. This did not work for me as I noticed in the debugger that the array which holds the dates have values different formats of the date values. For instance, some display as “16/11/2012” and some as (new Date(1355184000000)).
Can someone point a way to convert them all to a unified format so that I can work with them?
Thanks
How do you want your dates to be shown? Do you want them to be ‘date objects’ or strings? the value you show in your code (new Date(1355184000000)) correspond to
Mon Dec 10 16:00:00 PST 2012You can check that by using
Logger.log(new Date(1355184000000))On the contrary
"16/11/2012"is most probably not a date but a string…(note : strange that you use a “day/month/year” sequence since I saw on your profile you are in UK, I thought you’d use mm/dd/yyyy instead).Since you said you need to make some calculations on these items I guess that they all should be converted to date objects for using them in you script.
I’d suggest you look at some documentation on date object to see exactly how this should be done without generating errors. Don’t forget that dates in javascript are always dates and time in hh:mm:ss and milliseconds. The integer value you saw was the number of milliseconds since January the first in 1970 😉
You could also do a search on dates in this forum and find quite a lot of interresting informations.
Here is a small function to illustrate :
It will show this in the Logger :
btw, note that the Logger returns values in different ways, depending on the daylight saving in your timezone… I’m in Belgium and june is in ‘summer’ time (CEST). It can also be shown in PDT or PST which is the timezone of Google servers. You can’t rely on the logger to be constant (!!) but that’s another story 😉
EDIT : If your date strings are in the form dd/mm/yyyy then you should probably reorder it like in this code :
Which returns