C:\_G\WWW\~ELISANET\INFO\tscmd196.html
<https://www.salminet.fi/info/tscmd196.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 28-Aug-2026 10:31:56

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



196} How many days ago was a date? (More generally the difference between two dates)

Date tasks can also be approched using PowerShell-aided cmd.exe command-line.
  @echo off & setlocal enableextensions
  call :GetDatePS today_
  echo %%today_%%=%today_%
  ::
  set start_=05.06.2026
  set endat_=%today_%
  ::
  call :DateDiffPS "%start_%" "%endat_%" NumOfDays_
  echo Start date "%start_%"
  echo End date "%endat_%"
  echo %%NumOfDays_%%=%NumOfDays_%%
  endlocal & goto :EOF

  :: The indentation is essential to avoid incorrect syntax   
  :GetDatePS
  setlocal
  for /f "usebackq delims=" %%A in (`powershell -NoLogo -NoProfile -Command ^
    "Get-Date -Format 'dd.MM.yyyy'"`) do (
      set "result_=%%A")
  endlocal & set "%1=%result_%" & goto :EOF

  :DateDiffPS
  setlocal
  for /f "usebackq delims=" %%a in (`
    powershell -NoProfile -NoLogo -Command ^
      "$s=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
      "$e=[datetime]::ParseExact('%~2','dd.MM.yyyy',$null);"
      "($e-$s).Days"`) do (set result_=%%a)
  endlocal & set "%3=%result_%" & goto :EOF

The output might be e.g.
  C:\_M>C:\_D\TEST\CMDFAQ.CMD
  %today_%=21.08.2026
  Start date "05.06.2026"
    End date "21.08.2026"
  %NumOfDays_%=77

For more PowerShell-aided cmd.exe command-line solutions see #powershell.

The reverse PowerShell-aided cmd.exe command-line subroutine solution for the present item is also of interest.
  @echo off & setlocal enableextensions
  set startdate_=05.06.2026
  set adddate_=77
  call :GetDateThenPS "%startdate_%" "%adddate_%" datethen_
  echo The date %adddate_% days after %startdate_% is %datethen_%
  endlocal & goto :EOF

  :: The indentation is essential to avoid incorrect syntax
  :GetDateThenPS
  setlocal
  for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
    "$d=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
    "$d.AddDays(%~2).ToString('ddd dd.MM.yyyy')"`) do (
      set "result_=%%a")
  endlocal & set "%3=%result_%" & goto :EOF

The output is [the weekday is given in the user account's language]
  C:\_M>C:\_D\TEST\CMDFAQ.CMD
  The date 77 days after 05.06.2026 is Fri 21.08.2026

As indicated, the outcome from the line
  "$d.AddDays(%~2).ToString('ddd dd.MM.yyyy')"`) do (
depends on the user account's language settings. There are alternatives like
  :GetDateThenPSinvariant
  setlocal
  for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
    "$d=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
    "$d.AddDays(%~2).ToString('ddd dd.MM.yyyy',[Globalization.CultureInfo]::InvariantCulture)"`) do (
      set "result_=%%a")
  endlocal & set "%3=%result_%" & goto :EOF

  :GetDateThenPSus
  setlocal
  for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
    "$d=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
    "$d.AddDays(%~2).ToString('ddd dd.MM.yyyy',[CultureInfo]::GetCultureInfo('en-US'))"`) do (
      set "result_=%%a")
  endlocal & set "%3=%result_%" & goto :EOF

Instead of ('en-US') we could define e.g. ('fi-FI') or ('sw-SE') or even (sv-FI) etc.

Also see item#006 for the earlier, less generic code for getting yesterday's and tomorrow's date.
For other solutions (e.g. using VBS, gawk, or only original cmd script commands) see item#031 and item#061

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd196.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd196
C:\_E\ARCZIP\COPYTONE.CMD /~info
https://www.salminet.fi/info/tscmd196.html
file:///c:/_g/www/~elisanet/info/tscmd196.html