cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

It is summer time again

henk_stobbe
DynaMight Leader
DynaMight Leader

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

 

4 REPLIES 4

yanezza
Dynatrace Mentor
Dynatrace Mentor

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}`);



Yanez Diego Parolin

henk_stobbe
DynaMight Leader
DynaMight Leader

Cool, thanks. Yep I am using this in a WF😊

ChristopherHejl
Dynatrace Advisor
Dynatrace Advisor

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") }}

ChristopherHejl_0-1744012103748.png

ChristopherHejl_1-1744012111737.png

 

henk_stobbe
DynaMight Leader
DynaMight Leader

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

 

Featured Posts