Print out month using GlideDateTime

Jieun Jeong
Giga Contributor

Hi, I wrote a script, and I used GlideDateTime to print out the date(start_date) and the month of the date, but the output is different on the first day of each month. Could you tell me why?

 

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Jieun,

Probably because it's using JVM timezone. As written in the document, use getMonthUTC() instead.

str = '2021-02-01 00:00:10';
var gdt = new GlideDateTime();
//gdt.setDisplayValue(str);
gdt.setValue(str);
gs.print('gdt:' + gdt);
gs.print('getValue:' + gdt.getValue());
gs.print('getDisplayValue:' + gdt.getDisplayValue());
gs.print('getMonth:' + gdt.getMonth());
gs.print('gdtMonthLocalTime:' + gdt.getMonthLocalTime());
gs.print('gdtMonthUTC:' + gdt.getMonthUTC());

Execution result:

*** Script: gdt:2021-02-01 00:00:10
*** Script: getValue:2021-02-01 00:00:10
*** Script: getDisplayValue:2021-02-01 09:00:10
*** Script: getMonth:1
*** Script: gdtMonthLocalTime:2
*** Script: gdtMonthUTC:2

 

getMonth()

Retrieves the month stored by the GlideDateTime object, expressed in Java Virtual Machine time zone.

Use getMonthLocalTime() and getMonthUTC() instead of this method.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideDateTimeAPI#r_...

 

View solution in original post

3 REPLIES 3

Tapish Sharma1
Kilo Sage

Hi,

getMonth() will give you the month number not month name.

 

 

Kindly mark helpful/correct if my I answered your query

Community Alums
Not applicable

Hi @Jieun Jeong ,

You can try something like this, it would definitely help:

 

var gd = new GlideDate();

gs.print(gd.getByFormat("MMMM"));

 Output :-

10:48:40.861: April

or if you want the Name and Day of Month

Incoming Format: new GlideDateTime();
Simple Format: EEEE dd MMMMM

var gDate = new GlideDateTime().getDate();
gs.print(gDate.getByFormat('EEEE dd MMMMM'));
Thursday 14 December

 

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Jieun,

Probably because it's using JVM timezone. As written in the document, use getMonthUTC() instead.

str = '2021-02-01 00:00:10';
var gdt = new GlideDateTime();
//gdt.setDisplayValue(str);
gdt.setValue(str);
gs.print('gdt:' + gdt);
gs.print('getValue:' + gdt.getValue());
gs.print('getDisplayValue:' + gdt.getDisplayValue());
gs.print('getMonth:' + gdt.getMonth());
gs.print('gdtMonthLocalTime:' + gdt.getMonthLocalTime());
gs.print('gdtMonthUTC:' + gdt.getMonthUTC());

Execution result:

*** Script: gdt:2021-02-01 00:00:10
*** Script: getValue:2021-02-01 00:00:10
*** Script: getDisplayValue:2021-02-01 09:00:10
*** Script: getMonth:1
*** Script: gdtMonthLocalTime:2
*** Script: gdtMonthUTC:2

 

getMonth()

Retrieves the month stored by the GlideDateTime object, expressed in Java Virtual Machine time zone.

Use getMonthLocalTime() and getMonthUTC() instead of this method.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideDateTimeAPI#r_...