Turning an esriFieldTypeDate field (an integer) into a string

12603
7
01-27-2011 11:47 AM
DanielYim
New Contributor II
For my web application, I have a FeatureSet that returns the attribute table of a feature layer, and in that table, there is a date field (esriFieldTypeDate) that I must print. However, when I try that, I get an 11- to 12-digit integer instead of the typical MM/DD/YYYY date string.

The strange thing is, when I use the Query Related Records tool from the ArcGIS Services page, it actually parses the date field correctly... but the source of that page doesn't give any clues as to how it did.

So, for instance, I get the following:

51494400000 instead of 1971/08/20 00:00:00 UTC
50889600000 instead of 1971/08/13 00:00:00 UTC
779328000000 instead of 1994/09/12 00:00:00 UTC

Is there a pattern I'm not seeing? Is there a special way to parse these integer dates?

Thanks
7 Replies
HemingZhu
Occasional Contributor III
For my web application, I have a FeatureSet that returns the attribute table of a feature layer, and in that table, there is a date field (esriFieldTypeDate) that I must print. However, when I try that, I get an 11- to 12-digit integer instead of the typical MM/DD/YYYY date string.

The strange thing is, when I use the Query Related Records tool from the ArcGIS Services page, it actually parses the date field correctly... but the source of that page doesn't give any clues as to how it did.

So, for instance, I get the following:

51494400000 instead of 1971/08/20 00:00:00 UTC
50889600000 instead of 1971/08/13 00:00:00 UTC
779328000000 instead of 1994/09/12 00:00:00 UTC

Is there a pattern I'm not seeing? Is there a special way to parse these integer dates?

Thanks


using javascript date object to convert milliseconds to string date format like this
var date_string= new Date(milliseconds).toUTCString();
0 Kudos
DanielYim
New Contributor II
Thank you for the suggestion, hzhu.

My solution ended up being like this:

var epoch = new Date(attr.Date);
inputStr += (epoch.getMonth() + 1) + "/" + (epoch.getDate() + 1) + "/" + epoch.getFullYear();

It ended up being Unix Epoch time (seconds since January 1, 1970).
0 Kudos
DrewDowling
Occasional Contributor III
Daniel

I have been stuck on this all afternoon. My day and month was always one below what it should have been. Adding plus one as you did solves the problem, thanks. My question is: Why do we have to do this? Is there a bug in the data conversion or am I doing it wrong?

_covDate = new Date(myAttributes.RecordDate);
<s:Label text=" {_covDate.getMonth() + '/' + _covDate.getDate() + '/' + _covDate.getFullYear() }"/>
0 Kudos
JeffPace
MVP Alum
Daniel

I have been stuck on this all afternoon. My day and month was always one below what it should have been. Adding plus one as you did solves the problem, thanks. My question is: Why do we have to do this? Is there a bug in the data conversion or am I doing it wrong?

_covDate = new Date(myAttributes.RecordDate);
<s:Label text=" {_covDate.getMonth() + '/' + _covDate.getDate() + '/' + _covDate.getFullYear() }"/>


I agree we shouldnt have to do this (we shouldnt have to manually decode domain values either, but we do)

I get the layer description on my queries from the rest service, then check the field type
if(ldf.type=="esriFieldTypeDate"){
    field_val=   dojo.date.locale.format(new Date(field_val), {datePattern: "MM/dd/yyyy"});
                              }
0 Kudos
JohnGrayson
Esri Regular Contributor
I would agree with Jeff, use dojo.date.locale to properly format the date values as strings.  Below are some links that might be of some help:

http://api.dojotoolkit.org/jsdoc/1.6/dojo.date.locale.format

http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

dojo.require("dojo.date.locale");

_covDate = new Date(myAttributes.RecordDate);

var _covDateStr = dojo.date.locale.format(_covDate, {
  selector: 'date',
  datePattern: 'MM/dd/yyyy' 
});
0 Kudos
timgogl
New Contributor II
Daniel
Adding plus one as you did solves the problem, thanks. My question is: Why do we have to do this? Is there a bug in the data conversion or am I doing it wrong?


javascript holds the months (and i think days too) in arrays, the arrays are 0 based. so month[0] is january and month[11] is december so unless you add the 1 your month would actually be off by 1.
0 Kudos
DrewDowling
Occasional Contributor III
Ooops didn't realize I was posting in the Javascript form, I'm actually having this problem in flex:o. But good to know it is not api specific.

Just for the record Flex has a data formater object that I think is the same as the dojo.date.locale.format function, but just adding one will probably be less overhead so I continue to use that option.


The zero based array method of storing date and month makes perfect sense. Thanks for clearing that up Tim.
0 Kudos