我在时区中列出了这个时间段" -07:00"我想将它们转换为" +05:00"。
"hours": [
{
"from": "11:00",
"to": "21:00",
"dayOfWeek": "MONDAY"
},
{
"from": "11:00",
"to": "21:00",
"dayOfWeek": "TUESDAY"
},
{
"from": "11:00",
"to": "21:00",
"dayOfWeek": "WEDNESDAY"
},
{
"from": "11:00",
"to": "21:00",
"dayOfWeek": "THURSDAY"
},
{
"from": "11:00",
"to": "21:00",
"dayOfWeek": "FRIDAY"
}
]
如何使用像moment.js这样的库有效地做到这一点?我已经研究过一种方法,但它不符合标准。
function formatDates(hoursList, shopDate, userDate){
var moment = require('moment');
var selectedDateText = "";
var convertedDateList = [];
for (var x = 0; x < hoursList.length; x++) {
var fromDate = shopDate.clone();
var fromMoment = moment(fromDate.format(), "YYYY-MM-DD HH:mm Z");
var fromTime = fromMoment.utcOffset(userDate.utcOffset());
fromTime.hours(parseInt(hoursList[x].from.split(":")[0]));
fromTime.minutes(parseInt(hoursList[x].from.split(":")[1]));
fromTime.seconds(0);
fromTime.milliseconds(0);
console.log(fromTime.format());
var toDate = shopDate.clone();
var toMoment = moment(toDate.format(), "YYYY-MM-DD HH:mm Z");
var toTime = toMoment.utcOffset(userDate.utcOffset());
toTime.hours(parseInt(hoursList[x].to.split(":")[0]));
toTime.minutes(parseInt(hoursList[x].to.split(":")[1]));
toTime.seconds(0);
toTime.milliseconds(0);
console.log(toTime.format());
var initialDay = {
"from": "",
"to": "",
"dayOfWeek": ""
};
initialDay.dayOfWeek = fromTime.format("dddd").toUpperCase();
initialDay.from = fromTime.format("HH:mm");
if (toTime.format("DDD") != fromTime.format("DDD")) {
var secondaryDay = {
"from": "",
"to": "",
"dayOfWeek": ""
};
initialDay.to = "24:00";
secondaryDay.dayOfWeek = toTime.format("dddd").toUpperCase();
secondaryDay.from = "00:00";
secondaryDay.to = toTime.format("HH:mm");
convertedDateList.push(secondaryDay);
} else {
initialDay.to = toTime.format("HH:mm");
}
convertedDateList.push(initialDay);
}
console.log(convertedDateList);
return selectedDateText;
}
主要问题是,转换时的某些时间戳将在2个不同的日期。因此代码中的初始和次要日期。例如:当它大约6个小时被分成两个时,应该在周一晚上9点到12点之间以及周二12点到凌晨3点之间添加。如果它是一个小时数全部为7天且有些日子,转换后的时间从上午12点到凌晨3点到晚上12点甚至有几个小时,这就更难了。
如何妥善解决这个问题?
感谢。