Subscribe via Feed

SSJS @Adjust on Javascript Dates - Be Backwarned!

Jeremy Hodge, Mar 4, 2010 8:46:04 PM

The following code will generate results that you may not expect:

var myDate = new Date(2010,2,15);  //this is MARCH 15th, months for JSDate is zero based
myDate = @Adjust(myDate,0,-1,0,0,0,0);

You might expect myDate to now be 2/15/2010 ... its what the classic @Adjust @Formula would output.  But there is a little more than meets the eye going on here.

new Date(2010,2,15) creates a new Date in the local time zone... In SSJS @Adjust() by default treats the date as a UTC timezone, so the results you will actually get is:

2/14/2010

Why you ask? Because we didnt specify a time in the date object, it's set to 12 Midnight, and going from March back to February crosses the line for Daylight savings, and the adjusted time changes from 12:00:00 midnight to 11:00:00 PM the previous day as you cross back.

The key is to specify the "[InLocalTime]" keyword to the @Adjust function, so the correct call would be:

@Adjust(myDate,0,-1,0,0,0,0,"[InLocalTime]")

Thanks to Dave Connelly and the IBM Team for chasing this one down!

 


Paul Withers, Mar 4, 2010 9:20:04 PM

Ah, daylight savings, what fun it causes us all!




Post a new Comment

Name:


Comment: