0

I'm trying to show dates in popups in a leaflet map that consumes Map Service from ArcGIS Server. the date format is however in UNIX-standard (json: "Bedomdatum": 1228694400000,) and I'm lost when trying to convert these to readable format, such as 2017-12-31.

Any ideas or code snippets that I could learn from?

Link to Map service

I'm using L.esri.featureLayer to render the objects in the map https://atgarderivatten.se/karta_funktion

Excerpts of code:

    var fiskvagar = L.esri.featureLayer({
    url: 'https://ext-geodata.lansstyrelsen.se/arcgis/rest/services/WFS/LST_WFS_Miljodata/MapServer/2',
    pointToLayer: function (feature, latlng) {
            if(feature.properties.Bedomskick === "Gott") {
                return L.marker(latlng, {icon: Gott});
            }
            else {
                return L.marker(latlng, {icon: Bedomning_saknas});
            }
    },
}).addTo(map);

fiskvagar.bindPopup(function (evt) {
return L.Util.template('<h2>{FiskvNamn}</h2><table><small>\
<tr><td valign="top"><b>Datum:</b></td><td>{Bedomdatum}</td></tr>\
<tr><td valign="top"><b>ID / länk:</b></td><td><a href="{URL}" target="_blank">{FiskvagID}</a></td></tr>\
</small></table>', evt.feature.properties);

});

1

I'm lost when trying to convert these to readable format, such as 2017-12-31

Read the documentation for Date. After that, it's quite an easy task:

var timestampMsec = 1228694400000;
var myDate = new Date(timestampMsec);
var str = myDate.toDateString();

How to put that in your code? There are several ways. One of them would be something like

fiskvagar.bindPopup(function(evt){
    var featureDate = new Date(evt.feature.properties.Bedomdatum);
    return "Date: " + featureDate.toDateString();
});

If you need finer control over the format, I suggest having a look at https://momentjs.com/

  • 1
    Perfect. Thanks a lot. I added featureDate.toLocaleDateString to get the local format – Lunken May 2 '17 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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