Ich habe ein Datum, ich weiß, ist in Central Time gespeichert. Es hat das folgende Format: 2017-11-19T23:39:35.280000
. Ich möchte dies zu einem ISO-formatierten Datum in der aktuellen Zeitzone machen. Ich habe es zu arbeiten, wenn Sie einfach eine new Date()
erstellen - aber dann, wenn ich anschließend .toISOString()
aufrufen, geht es drunter und drüber. Hier ist der Code:Converting Central Time Datum in ISO-String in der aktuellen Zeitzone
function convertCentralToLocal() {
const centralOffset = 360;
const dateInCentralMs = new Date('2017-11-19T23:39:35.280000').getTime();
const now = new Date();
const localOffset = now.getTimezoneOffset();
// this works: Sun Nov 19 2017 23:39:35 GMT-0600 (CST)
const d = new Date(dateInCentralMs + ((centralOffset - localOffset) * 60000));
// this seems to give the date six hours off: 2017-11-20T05:39:35.280Z
const iso = d.toISOString();
}
Ist es ein Problem mit GMT? Ich fühle mich verloren.
"CST" Ich denke, ist US Central Standard Time, nicht Cuba Standard Time oder China Standard Time. Die US-CST ist UTC -600. Verwenden Sie nicht den integrierten Parser, siehe [* Warum gibt Date.parse falsche Ergebnisse? *] (Https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Die * toISOString * Methode ist immer UTC, beide Strings repräsentieren den gleichen Zeitpunkt, aber in verschiedenen Zeitzonen. Siehe auch [* Wo finde ich Dokumentation zur Formatierung eines Datums in JavaScript? *] (Https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-for-atting-a-date-in -javascript) – RobG