ISO Format conversion

SM16
Tera Expert

Hi Team,

 

I am trying to convert the given date in ISO format and its returning the data as well but date and time is in Europe/Amsterdam timezone and when you convert it to UTC/ISO, it should be less than two hours. So when time 13:05:57 is converted, it should return 

Hi Team,

 

I am trying to convert the given date in ISO format and its returning the data as well but date and time is in Europe/Amsterdam timezone and when you convert it to UTC/ISO, it should be less than two hours. So when time

15-04-2023 13:05:57 is converted to ISO, it should return 2023-04-15T11:05:57.000Z but it's giving something else

 

 

 

var gdt = new GlideDateTime('15-04-2023 13:05:57');
gs.print(gdt);
gs.print(gdt.getUserTimeZone());
var dt = gdt.getValue().split(' ');
var f = dt[0].split('-');
var t = dt[1].split(':');
var event = new Date(f[0], f[1]-1, f[2], t[0], t[1], t[2]);
var isoDate = event.toISOString();
gs.info(isoDate);

 

 Output

*** Script: 2023-04-15 13:05:57
*** Script: sun.util.calendar.ZoneInfo[id="Europe/Amsterdam",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Amsterdam,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
*** Script: 2023-04-15T20:05:57.000Z

 

1 ACCEPTED SOLUTION

Sai Shravan
Mega Sage

Hi @SM16 ,

When we create a new Date objet from these values, the timezone information is lost, and it defaults to the timezone of the browser or server running the script.

 

To fix this, you should explicitly set the timezone of the Date object to the timezone of the input value

 

var gdt = new GlideDateTime('15-04-2023 13:05:57');
gs.print(gdt);
gs.print(gdt.getUserTimeZone());
var dt = gdt.getValue().split(' ');
var f = dt[0].split('-');
var t = dt[1].split(':');
var event = new Date(Date.UTC(f[0], f[1]-1, f[2], t[0], t[1], t[2])); // Set timezone to UTC
var isoDate = event.toISOString();
gs.info(isoDate);

 

 

Regards,

Shravan.
Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

View solution in original post

1 REPLY 1

Sai Shravan
Mega Sage

Hi @SM16 ,

When we create a new Date objet from these values, the timezone information is lost, and it defaults to the timezone of the browser or server running the script.

 

To fix this, you should explicitly set the timezone of the Date object to the timezone of the input value

 

var gdt = new GlideDateTime('15-04-2023 13:05:57');
gs.print(gdt);
gs.print(gdt.getUserTimeZone());
var dt = gdt.getValue().split(' ');
var f = dt[0].split('-');
var t = dt[1].split(':');
var event = new Date(Date.UTC(f[0], f[1]-1, f[2], t[0], t[1], t[2])); // Set timezone to UTC
var isoDate = event.toISOString();
gs.info(isoDate);

 

 

Regards,

Shravan.
Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you