How to get 1)month , 2)year and 3)no of days in the current month from todays date

Avinash16
Tera Contributor

Hello All,

 

i need to get the current month name(ex January) , current year (ex 2023) and no of days in the current month(ex 31) from current date.

Iam using below script

var gdt2 = new GlideDateTime();
var gdt = gdt2.getValue();
 var month = gdt.getMonthUTC();
var year = gdt.getYearUTC();
 var days = gdt.getDaysInMonthUTC();

 

Here iam not getting the values for month year and no of days.

when i print i should get like  month = January 

                                                  year = 2023 

                                                  days = 31

Thanks&Regards,

Avinash

2 ACCEPTED SOLUTIONS

-O-
Kilo Patron
Kilo Patron

Since you are only interested in date (not time) details, there is no need to use GlideDateTime, GlideDate suffices. But GlideDateTime will also work. Running code

var gd = new GlideDate();

var dateInfo = {
	year: gd.getYearLocalTime(),
	month: getTranslatedMonthName(gd.getMonthLocalTime()),
	daysRemainingInMonth: gd.getDaysInMonthLocalTime() - gd.getDayOfMonthLocalTime(),
};

gs.debug(JSON.stringify(dateInfo, null, '\t'));

function getTranslatedMonthName (monthIndex) {
	return monthIndex > 0 && monthIndex < 13 ?
		gs.getMessage(new global.GregorianCalendarGenerator().MONTHS[monthIndex - 1]) :
		monthIndex;
}

in Scripts - Background results in output:

*** Script: [DEBUG] {
	"year": 2023,
	"month": "January",
	"daysRemainingInMonth": 19
}

 

View solution in original post

You're welcome!

As for the problem,

 

daysRemainingInMonth: gd.getDaysInMonthLocalTime() - gd.getDayOfMonthLocalTime(),

 

needs to be "simplified" to:

 

daysRemainingInMonth: gd.getDaysInMonthLocalTime(),

 

View solution in original post

3 REPLIES 3

-O-
Kilo Patron
Kilo Patron

Since you are only interested in date (not time) details, there is no need to use GlideDateTime, GlideDate suffices. But GlideDateTime will also work. Running code

var gd = new GlideDate();

var dateInfo = {
	year: gd.getYearLocalTime(),
	month: getTranslatedMonthName(gd.getMonthLocalTime()),
	daysRemainingInMonth: gd.getDaysInMonthLocalTime() - gd.getDayOfMonthLocalTime(),
};

gs.debug(JSON.stringify(dateInfo, null, '\t'));

function getTranslatedMonthName (monthIndex) {
	return monthIndex > 0 && monthIndex < 13 ?
		gs.getMessage(new global.GregorianCalendarGenerator().MONTHS[monthIndex - 1]) :
		monthIndex;
}

in Scripts - Background results in output:

*** Script: [DEBUG] {
	"year": 2023,
	"month": "January",
	"daysRemainingInMonth": 19
}

 

Avinash16
Tera Contributor

Hi,

Thank you very much for the response.

These are working but for the last one i need number of days in a current month not days remaining.

 

Regards,

Avinash

You're welcome!

As for the problem,

 

daysRemainingInMonth: gd.getDaysInMonthLocalTime() - gd.getDayOfMonthLocalTime(),

 

needs to be "simplified" to:

 

daysRemainingInMonth: gd.getDaysInMonthLocalTime(),