script timezone issue when using Date.now() to populate date fields
hello, i'm in detroit, it's 9:30pm on March 4 which means it is currently March 5 in GMT.
I have a Date field that I want to populate with Date.now() in a workflow script. when I do it, it sets 05 Mar 2026 as the date, not 04 Mar!
my youtrack profile (user setting) is Detroit and also my Global Setting is detroit.
to debug a bit, I wrote some timestamps to a text field. here is the code and result.
let n = Date.now();
let j = new Date();
ctx.issue.fields['My Text Field'] = 'now = ' + n + '\ndate = ' + j;
ctx.issue.fields['My Date Field'] = n;
output:
now = 1772678173581
date = Thu Mar 05 2026 02:36:13 GMT+0000 (GMT)
date field = 05 Mar 2026
i thought YouTrack was going to “know” that my user setting is Detroit and adjust to my timezone accordingly?
please let me know if I did something wrong. thanks
Please sign in to leave a comment.
Hi,
I'm Sergey from the YouTrack Team. Thanks for reaching out!
This is expected behavior. The
Date.now()function in workflows returns a UTC timestamp, and when you assign it to a Date field, YouTrack resolves the calendar date in UTC. Since Detroit is UTC−5, any time after 7:00 PM local time will already be the next day in UTC, which is why you're seeing March 5 instead of March 4.Your profile timezone setting does affect how DateTime fields are displayed, but it doesn't change how a raw timestamp maps to a Date field.
The good news is that the
date-timemodule has built-in timezone support that solves this nicely. Here's how you can use it:The
format()function converts the UTC timestamp to a date string in your timezone, andparse()converts it back to a timestamp that correctly represents that date. By passing'America/Detroit'explicitly, you ensure it works correctly regardless of which user triggers the workflow.Let me know if you have any more questions!
hey Sergey, thanks for the quick response!
it makes sense, but now next question is, do you have a way to programmatically check the user's timezone instead of hardcoding the script to use detroit? so i could make a function like this:
function CreateUnixTimestampInMyTimezone() {const todayStr = dateTime.format(Date.now(), 'yyyy-MM-dd', GET_TIMEZONE_FROM_USER_PROFILE);return dateTime.parse(todayStr, 'yyyy-MM-dd', GET_TIMEZONE_FROM_USER_PROFILE);}then, users of my youtrack instance who are NOT in detroit don't have a problem :)
thanks again
Hi!
In this case, you actually don't need to look up the timezone at all. Both
format()andparse()automatically use the current user's profile timezone when you omit thetimeZoneIdparameter. So your code simplifies to:This will respect each user's profile timezone automatically. Just note that for scheduled rules that aren't triggered by a user action, it falls back to the global default timezone setting instead.
Let me know if you have any more questions!