We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

DateAdd for Windows Command Prompt?

kevp75
kevp75 asked
on
Medium Priority
4,017 Views
Last Modified: 2012-05-06
I can get today's date in the format I need it, however now I need to subtract/add a month to it.

Can anyone help?


@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
 
@echo %Month%/%Day%/%Year%

Open in new window

Comment
Watch Question

Steve KnightIT Consultancy
CERTIFIED EXPERT
Commented:
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%B
    Set Day=%%A
    Set Year=%%C
)

Well.... taking just a month off isn't TOO hard using set /a.  

set /a lastmonth=%month%-1
set lastyear=%year%
if %lastmonth%==0 set lastmonth=12 & set lastyear=%year%-1
echo last month was %lastmonth%/%lastyear%

and then you just need to deal with whether it is January and turn the year back.

hth

Steve

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Steve KnightIT Consultancy
CERTIFIED EXPERT
Commented:
(I had to change dd and mm around as I use dd/mm/yyyy not mm/dd/yyyy in the for command).  If your month shows as 01 02 etc. rather than 1 2 etc. then we need to take that into account, also a typo error there above on testing with Jan date:

set month=01
set year=2009
set /a lastmonth=1%month%-101
set lastyear=%year%
if %lastmonth%==0 set lastmonth=12& set /a lastyear=%year%-1
echo last month was %lastmonth%/%lastyear%
Top Expert 2009
Commented:
I assume you're using preceeding 0 as in 02 for February. If you do you'll need to make the /a keep the 0 and also be able to do arithmetic functions for 08 and 09.
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
 
Set LastYear=%Year%
Set /a LastMonth=1%Month% - 1
if %LastMonth% EQU 100 Set /a LastYear-=1 & Set LastMonth=112
 
@echo Today: %Month%/%Day%/%Year%
@echo Last month: %LastMonth:~1%/%Day%/%LastYear%

Open in new window

Author

Commented:
thanks folks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.