How to find the first day of Month

Mohit Yadav
Tera Expert

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

8 REPLIES 8

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


Thats what I get for not checking the API documentation.


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.


James Freund
Tera Contributor

 

This should work just fine:

var sentDateTime = current.getValue('<the_sent_date_time_field>'),
    firstOfMonth = new GlideDateTime(sentDateTime)

firstOfMonth.setDayOfMonthUTC(1)