DOS Bat file that calculates and formats a date

Posted on 2012-08-10
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
    LVL 51

    Expert Comment

    by:Bill Prew
    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

    yes to both questions!
    LVL 3

    Assisted Solution

    by:John Smith
    LVL 51

    Accepted Solution

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now