• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1358
  • Last Modified:

Date into a dos environment variable

How do I get the current date into a dos environment variable?
Something like "set curdate=date /t"

1 Solution
Hi Tim.

The following batch files creates an environment variable with the name of "date" with the current date.

Use this batch file for any DOS, Windows 3.x, and Windows 9.x machine:

rem-------------------------  GetDate.BAT  --------------------------------
@echo off
echo.|date|find "Current" >cu##ent.bat
echo set date=%%4> current.bat
call cu##ent.bat
del cu??ent.bat > nul
rem-------------------------  GetDate.BAT  --------------------------------

If you have to run this on any Windows NT machine, then use this one instead:

rem-------------------------  GetDate.BAT  --------------------------------
@echo off
echo.|date|find "current" >t#e.bat
echo set date=%%5> the.bat
call t#e.bat
del t?e.bat > nul
rem-------------------------  GetDate.BAT  --------------------------------

After running the batch file, you'll have an environment variable with the name "date" holding the current date.

To test, at a DOS prompt type SET or SET DATE or ECHO %DATE%.

Best regards, Steve.
timsAuthor Commented:

Thanks Steve.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now