0

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)

Green Fire
  • 720
  • 1
  • 8
  • 21
  • 1
    You cannot convert time zones in the way you are trying to do here. See: https://stackoverflow.com/a/15171030/634824 – Matt Johnson-Pint May 19 '20 at 22:16
  • 3
    **Aside note:** stop, and use libraries for doing this, for example, momentjs. The js engines have different implementations. – Ele May 19 '20 at 22:16
  • Also, don't rely on the `Date` API for parsing strings. – Matt Johnson-Pint May 19 '20 at 22:16
  • @Ele - Preferably [Luxon](https://moment.github.io/luxon/) rather than Moment. – Matt Johnson-Pint May 19 '20 at 22:17
  • 1
    @MattJohnson-Pint Yes, I said libraries, so the OP can opt to use what s/he thinks it's better. – Ele May 19 '20 at 22:18
  • Sorry, I'll be clearer: On behalf of the Moment maintainers, of which I am one - please only use Moment for new projects. Luxon is in the Moment family, and we prefer you use it instead. Luxon is designed for modern applications. Moment is legacy. – Matt Johnson-Pint May 19 '20 at 22:29
  • @MattJohnson-Pint—don't you mean only use Moment for existing projects? – RobG May 19 '20 at 22:41
  • @MattJohnson-Pint Luxon looks nice but I think I will use moment because I am using ant-design (https://ant.design/) and some of the apis there use moment as default – Green Fire May 19 '20 at 22:42
  • 1
    Your "conversion" to another timezone is fine, the issue is expecting to parse it back correctly. The output of *toLocaleString* is implementation dependent and there is no requirement for the implementation that created it to correctly parse it back to a date (mostly because users can configure the string in ways the parser has no knowledge of). So your issue is parsing, not changing timezones. :-) – RobG May 19 '20 at 22:45
  • 2
    In regard to the 24:00 issue, change the language tag to "en-US-u-h23" to force the [hour cycle](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) to be 23 (under certain conditions DateTimeFormat defaults to a 24 hour cycle). – RobG May 19 '20 at 23:15

0 Answers0