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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could use getMonth and getYear.

Also see this very well documented API which also contains several nice code examples:
https://developer.servicenow.com/dev.do#!/reference/api/paris/server_legacy/c_GlideDateTimeAPI

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thanks Mark, i'll check it out

Jaspal Singh
Mega Patron
Mega Patron

Hi Tony,

 

When do you want it to be updated. When record is inserted/updated or client side? If you could elaborate exact case a better solution can be proposed.

Hi Jaspal,

 

With a business rule, when the user inserts or updates the record

 

Thanks!