DOS Bat file that calculates and formats a date

Posted on 2012-08-10
Medium Priority
Last Modified: 2012-09-05
I have an application that is passed a date range to be processed.  The format must be MM/DD/YYYY_MM/DD/YYYY where the '_' separates the start and end dates.

I need the start date to be the Monday of the current week and the end date to be the Sunday 4 weeks from the current date.

For example, if the script was run on Thu 8/9/2012, I would need the string:


Question by:gantone1
  • 2
  • 2
LVL 59

Expert Comment

by:Bill Prew
ID: 38281955
Is the Monday of the current week always the Monday that just went by?

And for the Sunday, you say 4 weeks from the current date, but does that mean go forward 4 weeks from today (which could be a Thursday say), and then go forward to the next Sunday?


Author Comment

ID: 38282329
yes to both questions!

Assisted Solution

by:John Smith
John Smith earned 1000 total points
ID: 38283345
LVL 59

Accepted Solution

Bill Prew earned 1000 total points
ID: 38284621
Okay, by far one of the easiest ways to do this is with a small helper program in vbscript.  VBS allows much better date manipulations.  Save the code below as a VBS file, and then you can run it from a BAT file to get the dates in the format you want as follows:

for /F "tokens=*" %%A in ('cscript //nologo c:\scripts\getdates.vbs') set MyDates=%%A

' Get current date, and weekday number relative to Monday = 1
dToday = Date()
iWeekday = Weekday(dToday, 2)

' Find the last Monday that went by
Select Case iWeekday
   Case 1 'Monday, nothing to do
      dMonday = dToday
   Case Else 'Tuesday to Sunday (2 to 7), adjust
      dMonday = DateAdd("d", (1-iWeekday), dToday)
End Select

' Calculate the Sunday just before the 5th Monday from the most recent one
dSunday = DateAdd("d", 34, dMonday)

' Return the dates in MM/DD/YYYY_MM/DD/YYYY format
Wscript.Echo Right("0" & Month(dMonday), 2) & "/" & Right("0" & Day(dMonday), 2) & "/" & Year(dMonday) & "_" & Right("0" & Month(dSunday), 2) & "/" & Right("0" & Day(dSunday), 2) & "/" & Year(dSunday)

Open in new window


Author Closing Comment

ID: 38370663
I have been too busy to try this, but in a week or two will have a chance.  Thanks for your help!

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question