Search code examples
windowsbatch-file

How assign the current day to a batch file variable and change the date?


I would like a script to execute:

date 01/DD/2017

Where DD is the current day of MM/DD/YYY

date /t returns: MM/DD/YYYY ^^^^^^^ 0123456 I think that:

%DATE:~3,% returns DD

How to assign the output of characters 3-4 to the variable DD and execute date 01/DD/2017? DD is a the two digit number of the current day.


Solution

  • you can maybe do something like:

    @echo off
    for /f "tokens=1" %%a in ('date /t') do set currentdate=%%a
    
    set DD=%currentdate:~3,2%
    
    date 01/%DD%/2017
    
    echo new date is set to 01/%DD%/2017
    

    or simply:

    date 01/%date:~3,2%/2017
    

    the same thing on linux in case someone need it you can do:

    #!/bin/bash
    DD=$(date +%d)
    echo "01/$DD/2017"
    

    I hope this help you bro.