02 Apr 2025 07:18 PM
Hello,
I really wat to use the local time in my workflow, new Date() always returns UTC.
Who knows how to do this?
KR Henk
Solved! Go to Solution.
05 Apr 2025 07:27 PM
Hi,
Are you talking about the JS code in workflow? The "new Date()" seems to be javascript sintax
Anyway, after getting the date in UTC, convert it in your timezone when you are getting the string.
let localDate = new Date();
let dateOptions = { timeZone: 'Europe/Rome' };
let localDateString = localDate.toLocaleDateString('en-US', dateOptions);
let localTimeString = localDate.toLocaleTimeString('en-US', dateOptions);
console.log(`Date: ${localDateString}`);
console.log(`Time: ${localTimeString}`);
06 Apr 2025 10:21 AM
Cool, thanks. Yep I am using this in a WF😊
07 Apr 2025 08:48 AM
You can also use the now() expression Expression reference — Dynatrace Docs
This supports timezones, returns a datetime object, which you can use to do timecalculation in your expression as well like so https://docs.dynatrace.com/docs/shortlink/automation-workflow-expression-reference#timedelta and you can serialize it into any kind of date/time format you like, eg:
{{ now('Europe/Vienna').strftime("%b %d %Y") }}
09 Apr 2025 07:42 PM
Both,
Thanks for your reply's.
I did selected (before reading your great suggestions):
const myDate = new Date();
let localDateString = myDate.toLocaleString("en-US", { timeZone: "Europe/Amsterdam" });
After this I converted back to an object:
let localDate = new Date(localDateString);
so later in the script I can use the well known methods like localDate.getHours();
Again thanks for your help!
KR Henk