8

I am working on a site that is using version 2.6 of the Esri JavaScript API. I have an info window that displays some date information, and the date is one day behind the date in the database.

For example:

If I identify a feature in ArcMap, the date returned is 8/15/2007. When I query for the same feature through the Rest endpoint I get 2007/08/15 00:00:00 UTC. When I identify the feature in the web application, the date returned is 8/14/2007.

In the JavaScript code I am formatting the date as follows:

${SALEDT:DateFormat(selector: 'date', fullYear: true)}

What is causing this, and what can I do to have the correct date returned?

0

1 Answer 1

7

Specify local: true in your date formatting function:

${SALEDT:DateFormat(selector: 'date', local: true, fullYear: true)}

Mintx's answer explains why you need to do this. More information on formatting info window/popup content is available in the help: Format info window content.

Edit: Use DateString, not DateFormat to specify the local option:

${SALEDT:DateString(local: true, hideTime: true)}

Edit 2: Thanks to the mods, here's the answer originally posted my Mintx that I refereced above:

You've got it mostly right, you just need to change the code to reflect the correct time zone. Since the feature you're reading from is a UTC timestamp, the JavaScript code is converting to your timezone, which (assuming you're in the US) will be 4 to 7 hours earlier than midnight of 8/15/2007, which is why it's returning one day earlier.

Edit 3: I threw together a quick, hacky way to do this with a custom formatting function: jsfiddle.net/yEkjm There is probably a better way to do this...I'm no expert on handling dates with JS.

8
  • Thanks Derek. I put local: true in the formatting function and am still getting dates one day behind. I verified that I have dojo.require("dojo.date.locale") - is there anything else I need? I verified that the setting/code isn't being cached. Commented Feb 14, 2012 at 18:22
  • 1
    That's my mistake, I used DateFormat in my answer and it should be DateString. I've edited my answer to show the correct syntax. Commented Feb 14, 2012 at 21:30
  • I tried using DateString earlier today, but I need the date in mm/dd/yyyy format, and DateString returns the date as "Tue August 14 2007." Commented Feb 14, 2012 at 22:33
  • @Tim if you want fine grained control over the date format, we're back to DateFormat. In my testing, features retrieved from a service that stores dates as UTC are automatically converted to the client's timezone when displayed. Commented Feb 14, 2012 at 23:02
  • Thanks Derek. You're right, they are being converted to my time zone - that's why they're one day earlier. Is there a way to force the dates to be returned as UTC, since that date is the correct one? Commented Feb 15, 2012 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.