- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 11:21 AM
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?
Many Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 05:40 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 11:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 11:50 AM
Thanks Mark, i'll check it out

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 11:24 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 11:44 AM
Hi Jaspal,
With a business rule, when the user inserts or updates the record
Thanks!