Convert date field to legible date

03-26-2018 10:42 AM
Regular Contributor

I'm having trouble extracting the year from my date fields, mostly because it gets reformatted into that weird integer that doesn't look like a date. How do you get this type of integer to look like a date: 1518566400000

I tried using the Date constructor but I don't think I'm using it right (or maybe I can't use it for this purpose at all?). Date | API Reference | ArcGIS API for JavaScript 4.6 

My code queries features, and for each feature I want to extract the year from the observation date to put into a select HTML element (drop-down menu). This is the code I have:

//Get all the years in which data was collected
function getYears(response){
  var features = response.features;
  //Put date of each feature into an array
  var values ={
    var obsvDate = feature.attributes.ObsvDate; //this is the weird integer
    //Do something here to change the date to a legible format
    return myLegibleDate;
return values;

I've attempted something as simple as 

var obsvDate = new Date(feature.attributes.ObsvDate);

But when I try to print obsvDate to the console -- console.log(obsvDate) -- to see what it looks like, nothing prints. Total javascript newbie so it's possible I'm just doing something stupid.

0 Kudos
3 Replies
Occasional Contributor III

Hi Molley,

Check this blog. Hopefully it helps you. 

ArcGIS JavaScript Hack: Fixing Dates from ArcGIS Server 

0 Kudos
MVP Honored Contributor

Here's how to use JavaScript Date in one of the JS samples to show the formatted date. A note about the getMonth. It returns a number 0-11, so you have to add 1 to it to get the expected month.

var date = new Date(features[i].attributes.date_evt);
console.log(date.getMonth() + 1 + ' ' + date.getDate() + ' ' + date.getFullYear());
Frequent Contributor

The only thing I'll add to what's been previously mentioned is that you should make sure to check the results in all the browsers you anticipate having to support for your application. Dates & Javascript across different browsers can be a bit of a challenge and I've had correct results in Chrome but Internet Explorer produce incorrect results.