How to convert string to date in Apps-Script

I'm having trouble getting a date string (like "17/08/2012") before a date so that it can be used to compare another date.

I would like to introduce the date 08/17/2012 00:00:00 GMT on 08/17/2012 so that it can be used correctly for comparison. I thought this would be the easiest part of what I'm trying to do, but apparently not. See My current code below.

function dateToString(dateString) { var dateArray = dateString.split("/"); var year = dateArray[2]; var month = dateArray[1]; var day = dateArray[0]; var date = new Date(year, month - 1, day); return date; } 

This code is currently producing "Fri Aug 17 16:00:00 PDT 2012," and I have absolutely no idea why it speaks at 16:00. I have tried many different ways to do this in the last hour and still cannot figure out what is right. Any ideas how I can get it to convert correctly?

As always, any help is greatly appreciated.

+4
source share
1 answer

Your code is correct. 16 hours PDT is the time in the Pacific time zone, if I'm right, which is 9 hours earlier than GMT. You should check the script timezone and table settings so that all date operations are in order. Just keep in mind that the logger sometimes shows the PDT value, even when the settings are good (at least it happened to me quite often in the past ...) you can always choose the way to display date objects using Utilities.formatDate()

For more information about the date object, see this help page.

+4
source

All Articles