gpt4 book ai didi

javascript - 通过将所有日期转换为 ISOString 格式,将 Javascript 数组格式化为 JSON

转载 作者:行者123 更新时间:2023-12-03 06:57:44 25 4
gpt4 key购买 nike

我有 javascript 对象数组,其中包含键和值。我的数组中有很多日期,我想将整个数组转换为正确的 JSON,在此过程中我还将数组内的所有日期转换为 ISOString 格式。我只能使用 JQuery、UnderscoreJS 或 momentz 库。

我的 javascript 数组的初始格式:

{  
"primaryPerformerId":"122418",
"primaryGroupingId":"63913",
"primaryCategoryId":"1",
"name":"Test Concert Event",
"venueId":"82",
"placeConfigs":[
{
"placeConfigId":"1232392"
}
],
"defaultLocale":"en_US",
"metas":[
{
"templateId":"201",
"name":"Test Concert Event",
"locale":"en_US"
}
],
"unknownEventDateIndicator":"false",
"unknownEventTimeIndicator":"false",
"eventStartTime":"05/18/2016 08:04 PM",
"trueOnSaleDate":"05/18/2016 08:04 PM",
"firstPresaleDate":null,
"status":"active",
"dynamicAttributes":[

],
"lastChanceDate":"05/18/2016 08:04 PM",
"onSaleDate":"05/15/2016 08:04 PM",
"confirmDate":"05/16/2016 08:04 PM",
"earliestPossibleInhandDate":"05/16/2016 08:04 PM",
"latestPossibleInhandDate":"05/18/2016 08:04 PM"
}

预期格式:

{  
"primaryPerformerId":"122418",
"primaryGroupingId":"63913",
"primaryCategoryId":"1",
"name":"Test Concert Event",
"venueId":"82",
"placeConfigs":[
{
"placeConfigId":"1232392"
}
],
"defaultLocale":"en_US",
"metas":[
{
"templateId":"201",
"name":"Test Concert Event",
"locale":"en_US"
}
],
"unknownEventDateIndicator":"false",
"unknownEventTimeIndicator":"false",
"eventStartTime":"2016-05-18T20:04:00.000Z",
"trueOnSaleDate":"2016-05-17T20:03:00.000Z",
"firstPresaleDate":null,
"status":"active",
"dynamicAttributes":[

],
"lastChanceDate":"2016-05-18T20:04:00.000Z",
"onSaleDate":"2016-05-12T23:38:18.775Z",
"confirmDate":"2016-05-11T23:38:18.775Z",
"earliestPossibleInhandDate":"2016-05-10T20:04:00.000Z",
"latestPossibleInhandDate":"2016-05-11T20:04:00.000Z"
}

最佳答案

这应该可以。它使用正则表达式查找日期值,并使用自定义处理程序将其组合在一起。我还想指出,这会将时区计算为该脚本运行的任何系统时区。由于输出时间采用 UTC 格式,您可能需要事先确保时区正确。

var o = {  
"primaryPerformerId":"122418",
"primaryGroupingId":"63913",
"primaryCategoryId":"1",
"name":"Test Concert Event",
"venueId":"82",
"placeConfigs":[
{
"placeConfigId":"1232392"
}
],
"defaultLocale":"en_US",
"metas":[
{
"templateId":"201",
"name":"Test Concert Event",
"locale":"en_US"
}
],
"unknownEventDateIndicator":"false",
"unknownEventTimeIndicator":"false",
"eventStartTime":"05/18/2016 08:04 PM",
"trueOnSaleDate":"05/18/2016 08:04 PM",
"firstPresaleDate":null,
"status":"active",
"dynamicAttributes":[

],
"lastChanceDate":"05/18/2016 08:04 PM",
"onSaleDate":"05/15/2016 08:04 PM",
"confirmDate":"05/16/2016 08:04 PM",
"earliestPossibleInhandDate":"05/16/2016 08:04 PM",
"latestPossibleInhandDate":"05/18/2016 20:04"
};

document.body.innerText = JSON.stringify(o, function(key, value) {
var res;
if(res = /^\s*([0-9]{1,2})\s*\/\s*([0-9]{1,2})\s*\/\s*([0-9]{1,4})\s+([0-9]{1,2})\s*\:\s*([0-9]{1,2})(?:\s*(AM|PM))?\s*$/i.exec(value)) {
value = (o.defaultLocale === 'en_US' ?
new Date(res[3], res[1]-1, res[2], res[6] ? (res[6].toUpperCase() === 'PM' ? 12 : 0) + (res[4] === '12' ? 0 : parseInt(res[4])) : res[4], res[5]) :
new Date(res[3], res[2]-1, res[1], res[6] ? (res[6].toUpperCase() === 'PM' ? 12 : 0) + (res[4] === '12' ? 0 : parseInt(res[4])) : res[4], res[5])
).toISOString();
}
return value;
});

关于javascript - 通过将所有日期转换为 ISOString 格式,将 Javascript 数组格式化为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com