4 回答
TA贡献1891条经验 获得超3个赞
如果您不想使用任何库,那么您可以尝试以下方法:
您已经有了所需格式的日期 - (DD-MM-YYY HH:MM:SS)。
let utcTimeOffset = '+0630';
var d = new Date(
'20200908232201'.replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$2-$3-$1 $4:$5:$6'
) + utcTimeOffset
);
然后使用UTC上述日期将日期转换为 UTC:
var utcDate = new Date(
Date.UTC(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds()
)
);
最后使用以下格式以所需的格式打印日期 -
var MyDateString =
('0' + utcDate.getUTCDate()).slice(-2) +
'-' +
('0' + (utcDate.getUTCMonth() + 1)).slice(-2) +
'-' +
utcDate.getUTCFullYear() +
' ' +
('0' + utcDate.getUTCHours()).slice(-2) +
'-' +
('0' + utcDate.getUTCMinutes()).slice(-2) +
'-' +
('0' + utcDate.getUTCSeconds()).slice(-2);
console.log(MyDateString); //08-09-2020 16-52-01
let utcTimeOffset = '+0630';
var d = new Date(
'20200908232201'.replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$2-$3-$1 $4:$5:$6'
) + utcTimeOffset
);
var utcDate = new Date(
Date.UTC(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds()
)
);
var MyDateString =
('0' + utcDate.getUTCDate()).slice(-2) +
'-' +
('0' + (utcDate.getUTCMonth() + 1)).slice(-2) +
'-' +
utcDate.getUTCFullYear() +
' ' +
('0' + utcDate.getUTCHours()).slice(-2) +
'-' +
('0' + utcDate.getUTCMinutes()).slice(-2) +
'-' +
('0' + utcDate.getUTCSeconds()).slice(-2);
console.log(MyDateString);
TA贡献1810条经验 获得超4个赞
您可以尝试使用Luxon库,它实际上是 moment.js 的继承者,并且具有许多强大的日期处理功能。
特别是,我们可以使用DateTime.fromFormat函数来解析提供的日期。
const DateTime = luxon.DateTime;
const obj = {
"localTimeStamp": 20200908232201, //YYYYMMDDHHMMSS
"utcTimeOffset" : "+0630"
}
const dt = DateTime.fromFormat(obj.localTimeStamp + obj.utcTimeOffset, "yyyyMMddHHmmssZZZ", { setZone: true } );
console.log("Local time:", dt.toISO())
console.log("UTC Time:", dt.toUTC())
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js" integrity="sha512-OyrI249ZRX2hY/1CAD+edQR90flhuXqYqjNYFJAiflsKsMxpUYg5kbDDAVA8Vp0HMlPG/aAl1tFASi1h4eRoQw==" crossorigin="anonymous"></script>
TA贡献1835条经验 获得超7个赞
您可以对 localTimeStamp 进行切片
const obj = {
"localTimeStamp": "20200908232201",
"utcTimeOffset": "+0630"
};
let localTimeStamp = obj.localTimeStamp;
let utcTimeOffset = obj.utcTimeOffset;
let formattedDate = new Date(localTimeStamp.slice(0, 4), localTimeStamp.slice(4, 6) - 1, localTimeStamp.slice(6, 8), localTimeStamp.slice(8, 10), localTimeStamp.slice(10, 12), localTimeStamp.slice(12, 14));
let formattedDateToLocal = new Date(formattedDate + utcTimeOffset);
let formattedDateToUTC = new Date(formattedDate.toUTCString() + utcTimeOffset);
console.log(`UTC : ${formattedDateToUTC.getDate()}-${formattedDateToUTC.getMonth()}-${formattedDateToUTC.getFullYear()} ${formattedDateToUTC.getHours()}:${formattedDateToUTC.getMinutes()}:${formattedDateToUTC.getSeconds()}`)
console.log(`Local : ${formattedDateToLocal.getDate()}-${formattedDateToLocal.getMonth()}-${formattedDateToLocal.getFullYear()} ${formattedDateToLocal.getHours()}:${formattedDateToLocal.getMinutes()}:${formattedDateToLocal.getSeconds()}`)
TA贡献1815条经验 获得超13个赞
const obj = {
localTimeStamp: 20200908232201,
utcTimeOffset : "+0630"
};
const date = new Date((new Date(obj.localTimeStamp)).toUTCString() + obj.utcTimeOffset);
console.log(date.toUTCString());
console.log(date.toLocaleString());
添加回答
举报