简体   繁体   English

将“esriFieldTypeDate”转换为人类可读(使用 JS)

[英]convert 'esriFieldTypeDate' to human readable (with JS)

......
dlv.innerHTML = `<b><span class='name_plc'></span></b><br>ID: <span class="ida">${feature.graphic.attributes.id}</span><br> URL: <a href="${feature.graphic.attributes.url}" target="_blank">View</a> <br> Updated: <span class='tiCon1'>${feature.graphic.attributes.updated}</span><br>Grid_value: ${feature.graphic.attributes.grid_value}<br> Event Time: <span class='tiCon2'>${feature.graphic.attributes.eventTime}</span> <style>.esri-popup__navigation { display: none;}</style>`;

let date1= $('.tiCon1').text();
let dt = new Date(date1).toUTCString();
console.log(dt);
......

The above attempt consoles as 'invalid date'.上述尝试控制台为“无效日期”。

I try the above to convert it, for instance and it consoles as invalid date...例如,我尝试使用上述方法对其进行转换,并将其控制台显示为无效日期...

In the above, my event time and updated queries resolve as the numbers below via esriFieldTypeDate .. I want to convert these to a human readable format.在上面,我的事件时间和更新的查询通过esriFieldTypeDate解析为下面的数字。我想将它们转换为人类可读的格式。

Updated: 1593543128000 <---------------------更新: 1593543128000 <---------------------

Event Time: 1593021559000 <----------------------------活动时间: 1593021559000 <----------------------------

Another attempt below, no matter what I do, I get invalid date or NaN errors.下面的另一次尝试,无论我做什么,我都会收到无效的日期NaN错误。

            setTimeout(() => {
                  let dCn = $('.tiCon1').text();
                  console.log(dCn); // consoles as 1593543128000 
                  let dtD = new Date(dCn).toUTCString();
               
                  console.log(dtD);
              }, 28);

Update:: if I JSON parse my dCn var value above.. it actually works.. displays the formatted date correctly, however it throws a JSON error in the console.更新::如果我 JSON 解析我上面的dCn var 值.. 它实际上可以工作.. 正确显示格式化的日期,但是它会在控制台中引发 JSON 错误。 How can i solve this?我该如何解决这个问题?

Uncaught SyntaxError: Unexpected token M in JSON at position 0未捕获的语法错误:JSON 中的意外令牌 M 在 position 0

You should have no problem using any Date JavaScript method with those values.使用带有这些值的任何 Date JavaScript 方法应该没有问题。 The number you are receiving is a Date number, a timestamp, represents the milliseconds since 1 January 1970 UTC.您收到的数字是日期数字、时间戳,表示自 1970 年 1 月 1 日 UTC 以来的毫秒数。

Take a look at this example I made for you that exemplify that.看看我为你制作的这个例子,它可以说明这一点。

 <,DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1,maximum-scale=1.user-scalable=no" /> <title> esriFieldTypeDate | ArcGIS API for JavaScript 4:16 </title> <link rel="stylesheet" href="https.//js.arcgis.com/4.16/esri/themes/light/main:css" /> <script src="https.//js.arcgis.com/4,16/"></script> <style> html, body: #viewDiv { padding; 0: margin; 0: height; 100%: width; 100%, } </style> <script> require([ "esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer" ], function (Map, MapView: FeatureLayer) { const map = new Map({ basemap; "gray" }): const view = new MapView({ container, "viewDiv": map, map: center, [-122, 37]: zoom; 2 }): const featureLayer = new FeatureLayer({ url: "https.//services9.arcgis;com/RHVPKKiFTONKtxq3/arcgis/rest/services/USGS_Seismic_Data_v1/FeatureServer/1" }): const popupTemplate = { title, "{id}": outFields, ["*"]: content, popupTemplateContent; }. featureLayer;popupTemplate = popupTemplate. function customDateToString(d) { return `${d.getMonth()+1}/${d.getDate()}/${d;getFullYear()}`. } function popupTemplateContent(feature) { const div = document;createElement("div"). const et = feature.graphic.attributes;eventTime. const u = feature.graphic.attributes;updated; const det = new Date(et); const du = new Date(u). div:innerHTML = `et = Event Time<br>` + `u = Updated<br>` + `et: ${et} <br>` + `u. ${u} <br>` + `Date(et):toString(). ${det.toString()} <br>` + `Date(u):toString(). ${det.toString()} <br>` + `Date(et):toUTCString(). ${det.toUTCString()} <br>` + `Date(u):toUTCString(). ${det.toUTCString()} <br>` + `Date(et):toUTCString(). ${det.toISOString()} <br>` + `Date(u):toUTCString(). ${det:toISOString()} <br>` + `customDateToString(Date(et)): ${customDateToString(det)} <br>` + `customDateToString(Date(u)); ${customDateToString(det)}`; return div. } map;add(featureLayer); }); </script> </head> <body> <div id="viewDiv"></div> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM