How do I format the date field from a querytask results featureset.

3298
2
Jump to solution
09-10-2014 02:21 PM
ShaneLim
Occasional Contributor

I am using the  https://developers.arcgis.com/javascript/jssamples/query_nomap.html sample to get a html table of the feature attributes.  My date fields are being returned as numbers.  Is there anyway to catch get them back as date fields so they look like dates?

Shane

          

function showResults(results) {

    var resultItems = [];

    var resultCount = results.features.length;

    for (var i = 0; i < resultCount; i++) {

        var featureAttributes = results.features.attributes;

        for (var attr in featureAttributes) {

            var attrAlias = results.fieldAliases[attr];

            var attValue = featureAttributes[attr];

            if (attValue == null) {

                attValue = "";

            };

            resultItems.push("<tr><td class='width-fixed'>" + attrAlias + ":</td><td class='width-variable'>  " + attValue + "</td></tr>");

        };

        resultItems.push("<br>");

    };

    dom.byId("info").innerHTML = resultItems.join("");

};

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

Shane,

You can use dojo to format the date. Here's a fiddle that shows how this works:  Edit fiddle - JSFiddle  For additional formatting options see the dojo doc: dojo/date/locale::format() — The Dojo Toolkit - Reference Guide

View solution in original post

2 Replies
SteveCole
Frequent Contributor

This is how any date field value is returned. The string of numbers is a date in Epoch format. This page has a little summary about what that date format actually is as well as a converter. In order for you to display it as a date in a format familiar to you, you'll need to do a couple things to it- convert the numbers to a date & then format the date into a format you prefer ("MM/DD/YYYY");

Some pseudocode might be:

var theDateVal = attr ; //getting your Epoch format date value

theDate = new Date(theDateVal);

theFormattedDate = Date.toSrtring('M/d/yyyy'); //returns a date formatted like '9/10/2014'

So, for your actual code, you need to evaluate if the current attribute in the loop is a date field. If so, intercept the value and format it before you push it into the resultItems array.

One last thing about dates & JavaScript. They're horribly inconsistent between browsers. As with almost everything done in Internet Explorer, you will get an entirely different value than what you would from any other browser like Chrome or Firefox. Most likely, it will return a null or NaN value for a formatted date. I would highly recommend using a date library in your project like date.js to help combat the idiosyncrasies of cross-browser date formatting.

Probably a little more than you wanted to here but the short answer is that you need to re-format the date value before passing it to the resultItems array.

Steve

KellyHutchins
Esri Frequent Contributor

Shane,

You can use dojo to format the date. Here's a fiddle that shows how this works:  Edit fiddle - JSFiddle  For additional formatting options see the dojo doc: dojo/date/locale::format() — The Dojo Toolkit - Reference Guide