mbolotov / k6-intellij-plugin

Apache License 2.0
12 stars 3 forks source link

New Date() returns different format for RUN/DEBUG #21

Open AndrewASel opened 4 days ago

AndrewASel commented 4 days ago

During run test new Date() returns 2024-10-31T22:42:19.356Z

During debug test new Date() returns: Thu Oct 31 2024 18:42:19 GMT-0400 (Eastern Daylight Time)

image

mbolotov commented 3 days ago

That's strange. Can you check if you have some additional settings in k6 that could affect the date formatting? I'm unable to reproduce it using a common setup: image

and this is for debugging: Current date: Fri Nov 01 2024 10:08:05 GMT+0300 (GMT+03:00)

mbolotov commented 3 days ago

could you share a project to reproduce?

AndrewASel commented 3 days ago

Hello @mbolotov It definitely strange, I checked JS docs, new Date() should return format like Fri Nov 01 2024 10:08:05 GMT+0300 (GMT+03:00) https://www.w3schools.com/js/js_dates.asp

But looks like in K6 it works differently, I just created simple test in grafana test builder, only added console log for a date.

It also returns this formatted date: 2024-11-01T21:01:23.742Z image

Run it locally and format the same image

Maybe it related with date format settings, I checked on windows and ubuntu This is a simple test:

import { sleep } from 'k6'
import http from 'k6/http'

export const options = {
  stages: [
    { duration: '1m', target: 1 }
  ],
}

export default function main() {
  let date = new Date();
  console.log(date);  
  sleep(10)
}
AndrewASel commented 3 days ago

I not sure, probably this docs are related with it https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-time-string-format

21.4.1.32 Date Time String Format ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

mbolotov commented 2 days ago

Okay, I got it. The k6 itself and the debugger (Node.js) behave the same in fact. If you pass a date directly to console.log, it will be formatted using the ISO 8601. if you concatenate a date with other string, the runtime will call Date.toString() function which returns a string in the format Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)

So this code prints the same for both run and debug:

export default function () {
    let a= new Date()
    console.log(a)
    console.log("" + a)
}

image image

When you look at a date object in debug evaluation view, it obviously format a date using toString() function: image