Support for different timezone

Hi Im trying to get the current date and time in London. I have used this method. But it just prints my current timezone.
can you please help me with this?

   const date = new Date();

        date.toLocaleString('en-GB', {
            timeZone: 'Europe/London',
1 Like

Hi @ushanib,

I believe there is no standardized way to do this in ES6 JS. However, I think you can approach this in two different ways.

Either, you could bundle your k6 project with the moment js library as described in our step by-step instructions. This solution could work, but you might encounter compatibility issues in the process, as k6’s JS interpreter is not 100% compliant with ES6 and might not support all the features’ moment depends on.

Or, you could write a tiny function that does the timezone offsetting manually. I believe there is no standard way of doing this in ES6, indeed (feel free to point me to resources showing otherwise if relevant :bowing_man:):

function shiftDateTZ(date, utcOffset) {
  // Number of milliseconds since the ECMAscript epoch
  const localTime = date.getTime();

  // Difference between local time and UTC time in milliseconds
  const localOffset = date.getTimezoneOffset() * 60000;

  // UTC time in milliseconds
  const utc = localTime + localOffset;

  // Compute the time for the selected offset to UTC in milliseconds
  const newDateTime = utc + 3600000 * utcOffset;

  // Return a new Date object with the offseted time
  return new Date(newDateTime).toLocaleString();

export default function () {
  // Get the current date and time
  const date = new Date();

  // Let's look for the time at UTC-4 (New York)
  const offset = -4;

  // Shift the date to the selected time zone
  const newDate = shiftDateTZ(date, offset);

  // Display the date and time
  console.log(`time in New York: ${newDate}`);

You pass the function the date object you want to offset from and the timezone difference relative to UTC you wish, and it will return a new Date object matching the timezone you’re looking for. This function is not as user-friendly as what you’d find in momentJS as it requires you to manually pass the offset to UTC of the timezone you would like to convert the Date to, but it should be reliable.

Please let me know if that was helpful and if you need any more support :bowing_man:

1 Like

Any suggestion how to overcome when daylight saving occurs? Because offset will not always be the same. Im comparing the time with London and Melbourne time

Hi @ushanib

Good question! I’ve found this interesting SO thread demonstrating how to find out if one is in DST time or not. Maybe it’s worth giving it a try. Let me know how it goes :bowing_man:

Turns out, I found a working solution :tada:

By bundling k6 script(s) in an ES6 project bundled by webpack, I was able to use the moment-timezone library to get the kind of results you’re looking for.

Assuming you’re working on a unix-like machine, here’s how you can get access to the features you’re looking for:

  1. Clone the k6-template-es6 locally. This is a pre-built template repo to bundle your scripts in a way that would give them access to ES6 features/libraries k6 wouldn’t have access natively to otherwise.
  2. Change directory to it.
  3. Run npm install moment-timezone --save. This will add the moment-timezone dependency to it.
  4. Update the main.js (that’s essentially your k6 script) file to use timezones:
import moment from "moment-timezone";

export default function () {
  console.log(moment("America/Los_Angeles").format("ha z"));
  1. run npm run webpack to produce the bundled code in build/app.bundle.js
  2. run the bundle using k6 :tada:
INFO[0000] 1am PDT                                       source=console

I hope you find this helpful :bowing_man:

1 Like