How to substract 60 days that from current date

JRY
Mega Guru

Hello Everyone,

I want to subtract 60 days that means 8 weeks from the current date. I'm having week start date field only on Time Sheet, I just want to run scheduled job on every Monday I need to check time cards records from past 2 months that means 8 weeks i.e. 56 days, so if we take today's date then past date should come as 15th Feb.
 
I have tried below script but it's showing wrong.

var date_time = gs.nowDateTime();

var gdt = new GlideDate();
var date = gdt.getByFormat("yyyy-MM-dd");
gs.info('Todays Date ' +date);
gs.info('Date should be 2021-02-15');
gdt.addDays(-56);
gs.info('ADD Date ' +gdt.getDisplayValue());


var ed = new GlideDateTime(gdt);
ed.addDays(-30);
gs.info('Other Date ' +ed);
 
 
find_real_file.png
Can anyone help me where i'm going wrong.
 
Thanks,
JRY
4 REPLIES 4

vikas shukla
Kilo Guru

Hi,

It's Consider (30 Days) in Feb.

Regards,

Vikas

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can simply use this

var gdt = new GlideDate();
var date = gdt.getByFormat("yyyy-MM-dd");
gs.info('Todays Date ' + date);
gs.info('Date should be 2021-02-15');
var gdt1 = new GlideDateTime(date);
gdt1.addDaysUTC(-60);
gs.info('ADD Date ' + gdt1.getDate());

Output:

[0:00:00.051] Script completed in scope global: script


Script execution history and recovery available here


*** Script: Todays Date 2021-04-06
*** Script: Date should be 2021-02-15
*** Script: ADD Date 2021-02-05

60 days before today is 5th Feb so the same date came in output

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

Thanks for instant response but every time it does not like 60 days means 2 month, some months have 31 days, some have 30 days, some have 28 and I will run scheduled job on Monday so easy to fetch current Monday.
 
 
Thanks,
JRY

Hi Ankur,

Is there any solution for my issue, I will explain the issue here I'm having field Week Starts On which will show dates which starts from Monday. Here I'm trying to send an email notification to the users who missed their submission of time card in past 2 months (56 Days or 8 weeks), here I'm very specific 56 Days or 8 Weeks because if I take 60 Days then it will not show correct day/date because we have 31 Days or 30 Days or 28/29 Days it will affect to get the correct date. I'm trying to trigger email notification through scheduled Job which will trigger every Monday, and it will check past 8 weeks records in the timesheet whoever missed submitting their timesheet then it will send them reminder with the specific week date but your script working fine, but it has to take that week Monday as start of the week it's taking past 8 weeks or 56 days means!

For example : 

Todays Date 2021-04-08

ADD Date 2021-02-11 (-56 days from today) but here it has to show 2021-02-08(Monday of that week)

How to fetch it can you help me ?

Thanks

JRY