How to find the first day of Month
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2015 12:55 PM
Hi Friends,
My Question is...
If a user is filling the date on the form in a date/time type field , how can i find the first day of that month which is filled by the user on the form.
e.g. - if user is filling 2015-10-4, then i want to get 2015-10-1. or if user is filling 2016-10-04, than i want to get 2016-10-1.
How can i achieve this in a Business Rule.
Thanks in Advance.
Regards,
Mohit Yadav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2015 11:51 PM
Interesting enough that also the guideline in the wiki. In addition, it recommend using addDaysLocaTime or addDaysUTC instead if your ServiceNow version is Eureka or a newer one. Same will apply for the functions getDaysInMonth and getDayOfMonth.
3.51 addDays(int)
Adds a specified number of days to the current GlideDateTime object. A negative parameter subtracts days. This method was replaced by the addDaysLocalTime(int) and addDaysUTC(int) methods starting with the Eureka release.
Source: GlideDateTime - ServiceNow Wiki
I hope this is helpful
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2015 12:01 AM
Thats what I get for not checking the API documentation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2015 12:05 AM
Ah. It's converting to your preferred display format. That doesn't matter though- the display value of the Date doesn't impace the running of the script.
As Berny pointed out though, you need addDays(1 - sentDay) rather than subtractDays (which doesn't exist). You need to wrap that in a check for sentDay > 1, or just store sentDay +1 earlier on in the script, to account for users actually choosing the first day of that month.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2019 10:59 AM
This should work just fine:
var sentDateTime = current.getValue('<the_sent_date_time_field>'),
firstOfMonth = new GlideDateTime(sentDateTime)
firstOfMonth.setDayOfMonthUTC(1)