我有一个显示来自 XML 文件的数据的谷歌地图应用程序。当您将鼠标悬停在某物上时,它会返回一些信息,包括数据。XML文件中的日期是这样的......
<cap:expires>2013-05-02T00:00:00-05:00</cap:expires>
我有这段代码可以转换它....
function dateFromString(s) {
var bits = s.split(/[-T:+]/g);
var d = new Date(bits[0], bits[1]-1, bits[2]);
d.setHours(bits[3], bits[4], bits[5]);
// Get supplied time zone offset in minutes
var offsetMinutes = bits[6] * 60 + Number(bits[7]);
var sign = /\d\d-\d\d:\d\d$/.test(s)? '-' : '+';
// Apply the sign
offsetMinutes = 0 + (sign == '-'? -1 * offsetMinutes : offsetMinutes);
// Apply offset and local timezone
d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset())
// d is now a local time equivalent to the supplied time
return (d);
}
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var months =['January','February','March','April','May','June','July','August','September','October','November','December'];
var ampm = " am";
var dt = (dateFromString("yyyy-MM-dd'T'HH:mm:ssZ"));
var yr = dt.getFullYear();
var mth = dt.getMonth(); // months in Javascript are 0-11 so May is month 4
mth = months[mth];
var dte = dt.getDate();
var dy = dt.getDay(); // days are 0-6
dy = days[dy];
var hrs = dt.getHours();
var h1 = hrs;
var mins = dt.getMinutes();
if (hrs >= 12) {ampm = " pm"}
if (hrs >= 13) {hrs = hrs - 12}
if (h1 == 0) {hrs = 12}
if (hrs <10) {hrs = "0" + hrs}; // if leading zero desired
if (mins <10) {mins = "0" + mins};
var dtstring = dy + " " + mth + " " + dte + " " + yr + " " + hrs + ":" + mins + ampm;
当您对其进行硬编码时,它会很好地转换它。
var dt = (dateFromString( '2013-05-02T11:08:00-6:00'));
我的问题是如何以及在何处插入 XML 中的元素,以便它知道要转换什么?我将它设置为输出,我只是不确定将输入放在哪里进行转换。我已经包含了指向地图和 xml 文件的链接,以防有人需要查看完整代码以了解它是如何设置的。