Hi Folks.
I'm working on a new script and I have to account for data parameters. Quite simply, the start date needs to be the previous Monday, and the end date needs to be the day prior. This will run 5 (possibly 6) days a week. So taking this weeks runs for example...
Tuesday 8/8 was run from 10/31 through 8/7
Wednesday 8/9 was run from 10/31 through 8/8
Thursday 8/10 was run from 10/31 through 8/9
etc, until we reach Sunday 8/13 which will run from 10/31 to 8/12
Then we start again on Tuesday 8/12 which will run from 8/7 through 8/14
I have no experience working with dates in scripts so I'm not sure where to start. Any pointers are greatly appreciated.
Working with Dates
Moderators: Dorian (MJT support), JRL
- Dorian (MJT support)
- Automation Wizard
- Posts: 1354
- Joined: Sun Nov 03, 2002 3:19 am
- Contact:
Re: Working with Dates
I'm not sure I completely follow (as August 8th 2022 was a Monday, 2023 is a Tuesday though), but this may be a starting point :
Code: Select all
//Previous Monday
//What day of the week is it today?
DayOfWeek>Today
//Our target is Monday (day 2), so calculate how many days to go back..
Let>Difference=2-Today
//What date is it today?
GetDate>ThisDate
//Deduct %Difference% days..
DateAdd>ThisDate,D,Difference,PreviousMonday
MDL>The Previous Monday was %PreviousMonday%
//Prior Day
DateAdd>PreviousMonday,D,-1,DatePrior
mdl>The date prior was %DatePrior%
Yes, we have a Custom Scripting Service. Message me or go here
Re: Working with Dates
My bad... my dates in my example should read as follows...
Tuesday 11/8 was run from 10/31 through 11/7
Wednesday 11/9 was run from 10/31 through 11/8
Thursday 11/10 was run from 10/31 through 11/9
But I see where you are going with this. I'll probably have to wrap this in a lunch of if statements but I think this is a good starting point.
Thank you very much.
Tuesday 11/8 was run from 10/31 through 11/7
Wednesday 11/9 was run from 10/31 through 11/8
Thursday 11/10 was run from 10/31 through 11/9
But I see where you are going with this. I'll probably have to wrap this in a lunch of if statements but I think this is a good starting point.
Thank you very much.
- Dorian (MJT support)
- Automation Wizard
- Posts: 1354
- Joined: Sun Nov 03, 2002 3:19 am
- Contact:
Re: Working with Dates
Aaah, well I'm glad I wasn't being thick. Happy to have helped.
Yes, we have a Custom Scripting Service. Message me or go here
Re: Working with Dates
For anyone who's interested, here is my final code which ensures my start date is always two Monday's prior, and my end date is the previous day. This is for a routine which will be running Tuesday through Sunday. It could probably be prettier but it works.
Code: Select all
//Previous Monday *2
//What day of the week is it today?
DayOfWeek>Today
//Our target is Monday (day 2), so calculate how many days to go back..
Let>Difference=2-Today
//Deduct %Difference% days then -7
GetDate>ThisDate
DateAdd>ThisDate,D,Difference,PreviousMonday
DateAdd>PreviousMonday,D,-7,StartDate
//Prior Day
DateAdd>ThisDate,d,-1,EndDate