如何使用偏移量将 YYYYMMDDHHMMSS 转换为 UTC

我在具有 localTimeStamp 和 UTC 偏移量的对象中获取日期,如下所示。


obj = {

 "localTimeStamp": 20200908232201 //YYYYMMDDHHMMSS

 "utcTimeOffset" : "+0630"

}

如何将其转换为


本地日期时间 - 格式 DD-MM-YYYY HH:MM:SS


UTC 日期时间 - 格式 DD-MM-YYYY HH:MM:SS


我尝试过以下解决方案,但如何在 UTC 中转换。


"20200908232201".replace(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,'$2-$3-$1 $4:$5:$6');


慕村225694
浏览 204回答 4
4回答

万千封印

如果您不想使用任何库,那么您可以尝试以下方法:您已经有了所需格式的日期 - (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-01let 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);

慕莱坞森

您可以尝试使用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>

qq_花开花谢_0

您可以对 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()}`)

萧十郎

const obj = {&nbsp; localTimeStamp: 20200908232201,&nbsp; utcTimeOffset : "+0630"};const date = new Date((new Date(obj.localTimeStamp)).toUTCString() + obj.utcTimeOffset);console.log(date.toUTCString());console.log(date.toLocaleString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript