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:
<SPAN class="comment token">//Get all the years in which data was collected</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">getYears</SPAN><SPAN class="punctuation token">(</SPAN>response<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> features <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">//Put date of each feature into an array</SPAN>
<SPAN class="keyword token">var</SPAN> values <SPAN class="operator token">=</SPAN> features<SPAN class="punctuation token">.</SPAN><SPAN class="token function">map</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN>feature<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> obsvDate <SPAN class="operator token">=</SPAN> feature<SPAN class="punctuation token">.</SPAN>attributes<SPAN class="punctuation token">.</SPAN>ObsvDate<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//this is the weird integer</SPAN>
<SPAN class="comment token">//Do something here to change the date to a legible format</SPAN>
<SPAN class="comment token">//...</SPAN>
<SPAN class="comment token">//...</SPAN>
<SPAN class="keyword token">return</SPAN> myLegibleDate<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">return</SPAN> values<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>I've attempted something as simple as
<SPAN class="keyword token">var</SPAN> obsvDate <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Date</SPAN><SPAN class="punctuation token">(</SPAN>feature<SPAN class="punctuation token">.</SPAN>attributes<SPAN class="punctuation token">.</SPAN>ObsvDate<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
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.