Extract Month-Year from Date into a String

Tony M
Kilo Expert

Hi Everyone,

I want to extract the Month & Year from the date field 'opened_at' in the Task table into a custom string like u_month & u_year. Does someone knows how can I achieve this?

find_real_file.png

Many Thanks!

1 ACCEPTED SOLUTION

Are you getting any value for the month?

I just ran a slightly modified version in my usual test setup; a "display" business rule and outputting to an error message, like this:

var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
var gYear = gdt.getYear();

//current.u_month = gMonth.getValue();
//current.u_year = gYear.getValue();

gs.addErrorMessage("The year is " +gYear+ " and the month is " +gMonth);

With this I got a value of 1 for month when I opened an Incident record created in January. So, the month value will be 1 thru 12. Now if this is not suitable for your use and you would prefer it to be text, you will need to translate the values in the business rule.

And as for when to run the business rule. I have date calculating business rules that are running as "before" and also some running as "after" depending on the entire functionality in the script.

View solution in original post

8 REPLIES 8

Ian Mildon
Tera Guru

Something like this should do the trick:

var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
var gYear = gdt.getYear();

current.u_month = gMonth.getValue();
current.u_year = gYear.getValue();

Hi Ian,

 

I tried It many times with a business rule for the Month but it's not working, Do you know what could be wrong?

 

u_month is a string

When is before

 

(function executeRule(current, previous /*null when async*/) {

var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();

current.u_month = gMonth.getValue();

})(current, previous);

 

Thanks!

Are you getting any value for the month?

I just ran a slightly modified version in my usual test setup; a "display" business rule and outputting to an error message, like this:

var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
var gYear = gdt.getYear();

//current.u_month = gMonth.getValue();
//current.u_year = gYear.getValue();

gs.addErrorMessage("The year is " +gYear+ " and the month is " +gMonth);

With this I got a value of 1 for month when I opened an Incident record created in January. So, the month value will be 1 thru 12. Now if this is not suitable for your use and you would prefer it to be text, you will need to translate the values in the business rule.

And as for when to run the business rule. I have date calculating business rules that are running as "before" and also some running as "after" depending on the entire functionality in the script.

Many thanks Ian, it finally works as a Display rule.

 

Kind Regards