At a windows command line, when typing Date we’re asked to type a date value.
But what if we need to change the date to a value which is a memorized (and not type it)?
@echo off
set todaysdate=%date%
date 11/11/2012
date %todaysdate%
The last line shows this error: The system cannot accept the date entered. And then requires us to enter a date.
So what’s wrong? Do I need to change the variable’s type? Is it possible and how?
Update:
Problem: Thanks to dbenham, we now know that the problem lies with Date %todaysdate% and its alternative Set "Todaysdate=%DATE:/=-%" leading to: day of the week + Date.
Example: Date %todaysdate% and Set "Todaysdate=%DATE:/=-%" (at the 1st script line) will have value: Sat 11/24/2012. So when we ask date (at the last line) to have this value Sat 11/24/2012 it fails since date doesn’t accept this value which includes day of the week Sat.
Solutions: I now think of 2 solutions but I’m not sure if they’re applicable in cmd.
1st solution is to make a test to see if 1st script line set value includes day of the week (Sat, Sun…) and to trim day of the week.
2nd solution is to change type of 1st line variable, so whichever type the variable has we’ll change it to simple date without day of the week (Sun, Mon…)
when you set
todaysdatetake only the last 10 characters (mm/dd/yyyy or dd/mm/yyyy depends on your locale).this should work: