-
万千封印
如果您不想使用任何库,那么您可以尝试以下方法:您已经有了所需格式的日期 - (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 = { localTimeStamp: 20200908232201, utcTimeOffset : "+0630"};const date = new Date((new Date(obj.localTimeStamp)).toUTCString() + obj.utcTimeOffset);console.log(date.toUTCString());console.log(date.toLocaleString());