[RESOLVED] toLocaleString with timeZone does not work?

Hi,

I have two time/date fields and I'm trying to change timezone for the second field to show different timezone's time, but it seems that timezones does not work at all.

Is it a bug or feature?

Here is an example:

let date = new Date(1682931600000);
console.log("Dates:");
console.log(new Date(date).toLocaleString('en-US', {timeZone: 'Etc/UTC'}));
console.log(new Date(date).toLocaleString('en-US', {timeZone: 'Europe/Kyiv'}));
console.log(new Date(date).toLocaleString('en-US', {timeZone: 'Europe/Amsterdam'}));

The results are the same.

Dates:
Mon May 01 2023 09:00:00 GMT+0000 (GMT)
Mon May 01 2023 09:00:00 GMT+0000 (GMT)
Mon May 01 2023 09:00:00 GMT+0000 (GMT)

Expected results:

Dates:
5/1/2023, 9:00:00 AM
5/1/2023, 12:00:00 PM
5/1/2023, 11:00:00 AM
0
4 comments
Official comment

Hello Viacheslav. 

You can use the format function of the date-time for this purpose.

You will need to reference the module in the beginning of your workflow code:

const dateTime = require('@jetbrains/youtrack-scripting-api/date-time');

To provide an example, you can then paste the part below to the action section of your workflow rule:

    const timestamp = 1682931600000;
    const utc = dateTime.format(timestamp, null, "UTC");
    const utcPlus10 = dateTime.format(timestamp, null, "Australia/Sydney");
    
    console.log(utc);
    console.log(utcPlus10);

Then trigger the rule and see the result logged in the workflow console toolbar: 1 May 2023 09:00 and 1 May 2023 19:00.

Here is where you can find the list of the supported locale IDs (to be set as the 3rd argument of the format function, as shown on the example above): Available Time Zones.

Hi,

I need to convert date and time to timestamp and use it in date/time field. And this code does not work:

      const tz = require('@jetbrains/youtrack-scripting-api/date-time');
      
    const localTime = 1686258659000; // GMT: Thursday, 8 June 2023 г., 21:10:59
      
      const currentTimeKyiv = tz.format(localTime, null, "Europe/Kyiv");
      log += "!Current time in Kyiv: " + currentTimeKyiv + "\n";
      const localTimeEpoch = tz.parse(currentTimeKyiv, null, "Europe/Kyiv");
      log += "!Current ts__ in Kyiv: " + localTimeEpoch + "\n\n";
      
      const timeInUTC = tz.format(localTime, null, "UTC");
      log += "!Current time in UTC_: " + timeInUTC + "\n";
      const UTCTimeEpoch = tz.parse(currentTimeKyiv, null, "UTC");
      log += "!Current ts__ in UTC_: " + UTCTimeEpoch + "\n\n";
      
      const timeInAmsterdam = tz.format(localTime, null, "Europe/Amsterdam");
      log += "!Current time in Ams_: " + timeInAmsterdam + "\n";
      const AmsTimeEpoch = tz.parse(currentTimeKyiv, null, "Europe/Amsterdam");
      log += "!Current ts__ in Ams_: " + AmsTimeEpoch;
      
      console.log(log);

The timestamps are wrong:

!Current time in Kyiv: 9 Jun 2023 00:10
!Current ts__ in Kyiv: 1686258600000

!Current time in UTC_: 8 Jun 2023 21:10
!Current ts__ in UTC_: 1686269400000

!Current time in Ams_: 8 Jun 2023 23:10
!Current ts__ in Ams_: 1686262200000

What am I doing wrong?

0

Hi Viacheslav,

I think you forgot to change the variable names in the parse function argument (you have currentTimeKyiv in all three cases here). Also, unfortunately, the Kyiv spelling is not supported at the moment. As the documentation suggests, the zone IDs should be put as they appear in Joda's Available Time Zones. So right now, this should work instead:

    const currentTimeKyiv = tz.format(localTime, null, "Europe/Kiev");
    log += "!Current time in Kyiv: " + currentTimeKyiv + "\n";
    const localTimeEpoch = tz.parse(currentTimeKyiv, null, "Europe/Kiev");
    log += "!Current ts__ in Kyiv: " + localTimeEpoch + "\n\n";

    const timeInUTC = tz.format(localTime, null, "UTC");
    log += "!Current time in UTC_: " + timeInUTC + "\n";
    const UTCTimeEpoch = tz.parse(timeInUTC, null, "UTC");
    log += "!Current ts__ in UTC_: " + UTCTimeEpoch + "\n\n";

    const timeInAmsterdam = tz.format(localTime, null, "Europe/Amsterdam");
    log += "!Current time in Ams_: " + timeInAmsterdam + "\n";
    const AmsTimeEpoch = tz.parse(timeInAmsterdam, null, "Europe/Amsterdam");
    log += "!Current ts__ in Ams_: " + AmsTimeEpoch;

However, I've reached out to the team, and it appears there had been an update in tzdata behind the scenes, so Kyiv spelling not working is in fact a bug: JT-75387. Thank you for pointing this out.

0

Here is the correct way:

let dctimezone="Europe/Amsterdam";
const TZ_LOCAL="Europe/Kyiv";
const issue = ctx.issue;

if (issue.fields.isChanged(ctx.LocalStart) && issue.fields["Local Start Date"] !== null) {
const localTime = issue.fields["Local Start Date"];
const dcTime = TZ.format(localTime, null, dctimezone);
const dcTimeEpoch = TZ.parse(dcTime, null, TZ_LOCAL);
issue.fields["DC Start Date"] = dcTimeEpoch;
}

// ----
requirements: {
    LocalStart: {
      name: "Local Start Date",
      type: entities.Field.dateTimeType
    },
    DCStart: {
      name: "DC Start Date",
      type: entities.Field.dateTimeType
    }
}

And this code works, despite the fact that the time zone is specified as "Europe/Kyiv" and not "Europe/Kiev".

0

Please sign in to leave a comment.