how to convert date /time to ist timezone and also change format?

Servicenow10
Kilo Guru

I am using glidedatetime function and getting the date/time....it is coming in gmt timezone,but i need in ist.

1 ACCEPTED SOLUTION

you can do it like this.

//assuming start is the date/time object as per above code
var st_date = start.getDate();
var st_time = start.getTime();
var start_date = st_time.getByFormat("HH:mm") +" "+ st_date.getByFormat("dd MMMM YYYY");
gs.print(start_date);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

5 REPLIES 5

asifnoor
Kilo Patron

Hi, 

Try this code.

var gdt = new GlideDateTime();
var gdt1 = new GlideDateTime();
//sets to IST
var tz = Packages.java.util.TimeZone.getTimeZone("IST");
gdt1.setTZ(tz);
gdt1.setValue(gdt); //in place of gdt, you can mention your date/time object variable
var start =gdt1.getDisplayValue(); //returns the date/time in IST timezone as a string.
start = new GlideDateTime(start); //returns the date/time in IST in date/time object

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

and also how can i format the date an time in this manner : (HH:ss) (DD-MM-YYYY)

you can do it like this.

//assuming start is the date/time object as per above code
var st_date = start.getDate();
var st_time = start.getTime();
var start_date = st_time.getByFormat("HH:mm") +" "+ st_date.getByFormat("dd MMMM YYYY");
gs.print(start_date);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Namrata Khabale
Giga Guru

Hey dell,

 

Here is a same thread that you looking for it will help you:

https://community.servicenow.com/community?id=community_question&sys_id=2f2fb4651bb48810fff162c4bd4b...

Or 

 

Try this out:

setTZ(TimeZone timeZone)
Sets the time zone of the GlideDateTime object to be the specified time zone.

var tz = gs.getSession().getTimeZone();
var gdt = new GlideDateTime();
gdt.setTZ(tz);

 

Kindly Mark Correct if this solves your issue and also mark  Helpful if you find my response worthy!

 

 

Best Regards,

Namrata