为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村225694 2023-07-06 16:39:20
我在具有 localTimeStamp 和 UTC 偏移量的对象中获取日期,如下所示。obj = { "localTimeStamp": 20200908232201 //YYYYMMDDHHMMSS "utcTimeOffset" : "+0630"}如何将其转换为本地日期时间 - 格式 DD-MM-YYYY HH:MM:SSUTC 日期时间 - 格式 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');
查看完整描述

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);


查看完整回答
反对 回复 2023-07-06
?
慕莱坞森

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>


查看完整回答
反对 回复 2023-07-06
?
qq_花开花谢_0

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()}`)



查看完整回答
反对 回复 2023-07-06
?
萧十郎

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());


查看完整回答
反对 回复 2023-07-06
  • 4 回答
  • 0 关注
  • 203 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信