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

How to convert YYYYMMDDHHMMSS to UTC using offset

我在对象中获取具有如下 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)$/,'-- ::');

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

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

您可以对 localTimeStamp 进行切片(我从 Christoph 的回答中得到了这个令人赞同的想法 Christoph

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

如果您不想使用任何库,那么您可以尝试以下方法:

您已经有了所需格式的日期 - (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)$/,
    '-- ::'
  ) + utcTimeOffset
);

然后使用上面日期的UTC将日期转换为UTC:

var utcDate = new Date(
  Date.UTC(
    d.getUTCFullYear(),
    d.getUTCMonth(),
    d.getUTCDate(),
    d.getUTCHours(),
    d.getUTCMinutes(),
    d.getUTCSeconds()
  )
);

最后使用下面的格式打印所需的日期 fromat -

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)$/,
    '-- ::'
  ) + 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);