问题
After isolating the problem in my current project .... I think something is not working currently with the Date object in javascript.
But I don't want to be arrogant because maybe I am doing something wrong, is it a bug? or am I doning something wrong?
I want to convert a date to diffrent time zone in javascript, I made this function:
function ConvertToTimeZone(date, timezone) {
let convertedTimeString = date.toLocaleString("en-US", {hour12: false, timeZone: timezone})
return new Date(convertedTimeString)
}
Then I am using it like this:
let testDate = new Date("Thu May 21 2020 17:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "Europe/London");
Or like this:
let testDate = new Date("Thu May 21 2020 18:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "UTC");
But something wrong is happening when I use this date:
let testDate = new Date("Thu May 21 2020 17:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "UTC");
with that last exmaple convertedDate
end up with Invalid Date {}
And when I look closely in my ConvertToTimeZone
the result of date.toLocaleString(...)
is 5/22/2020, 24:04:05
which looks strange because the hours of that time cannot be 24 (it should be 0)
来源:https://stackoverflow.com/questions/61901653/converting-timezones-in-javascript-resulted-in-very-strange-results-am-i-doing