How to Get the Date and Time in Batch Script
- Using the %DATE% and %TIME% Variables
- Formatting Date and Time
- Combining Date and Time
- Conclusion
- FAQ

Getting the current date and time in a Batch Script is an essential skill for anyone looking to automate tasks on Windows. Whether you’re logging events, creating backups, or simply needing to timestamp files, understanding how to retrieve this information can streamline your workflow.
In this article, we’ll explore various methods to obtain the date and time using Batch Script with clear code examples. By the end, you’ll have a solid grasp of how to effectively utilize these commands in your scripts, making your automation tasks more efficient and organized.
Using the %DATE% and %TIME% Variables
The simplest way to get the current date and time in a Batch Script is by using the built-in environment variables %DATE%
and %TIME%
. These variables automatically fetch the system’s current date and time when called.
Here’s how you can use them:
@echo off
set currentDate=%DATE%
set currentTime=%TIME%
echo Current Date: %currentDate%
echo Current Time: %currentTime%
Output:
Current Date: Wed 10/11/2023
Current Time: 14:45:30.12
In this example, we first turn off the command echoing with @echo off
for cleaner output. We then set two variables: currentDate
and currentTime
, which store the values of %DATE%
and %TIME%
, respectively. Finally, we print these variables to the console. The output will display the current date and time in the format specified by your system’s regional settings. This method is quick and straightforward, making it ideal for simple scripts where you need the date and time without additional formatting.
Formatting Date and Time
While the %DATE%
and %TIME%
variables are handy, you might need the date and time in a specific format for logging or file naming purposes. To achieve this, you can manipulate the output using string operations.
Here’s an example that formats the date and time:
@echo off
for /f "tokens=1-3 delims=/ " %%a in ("%DATE%") do (
set day=%%a
set month=%%b
set year=%%c
)
for /f "tokens=1-2 delims=: " %%a in ("%TIME%") do (
set hour=%%a
set minute=%%b
)
set formattedDate=%year%-%month%-%day%
set formattedTime=%hour%-%minute%
echo Formatted Date: %formattedDate%
echo Formatted Time: %formattedTime%
Output:
Formatted Date: 2023-10-11
Formatted Time: 14-45
In this script, we use for /f
loops to parse the %DATE%
and %TIME%
outputs. The delims
option specifies the characters used to split the input. We then assign the parsed values to new variables for day, month, year, hour, and minute. Finally, we create formatted strings for the date and time, which are printed out. This method allows you to customize the output to fit your needs, making it suitable for scenarios like creating timestamped logs or files.
Combining Date and Time
Sometimes, you may want to combine the date and time into a single string for logging or display purposes. This can be done easily by concatenating the formatted strings.
Here’s how you can do it:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-3 delims=/ " %%a in ("%DATE%") do (
set day=%%a
set month=%%b
set year=%%c
)
for /f "tokens=1-2 delims=: " %%a in ("%TIME%") do (
set hour=%%a
set minute=%%b
)
set combinedDateTime=!year!-!month!-!day! !hour!:!minute!
echo Combined Date and Time: !combinedDateTime!
Output:
Combined Date and Time: 2023-10-11 14:45
In this script, we again extract the date and time components using for /f
loops. Here, we use setlocal enabledelayedexpansion
to allow the use of !
for variable expansion inside a loop. We then concatenate the date and time into a single variable called combinedDateTime
. The output is a neatly formatted string that combines both elements, making it perfect for logs or other applications where a timestamp is required.
Conclusion
In this article, we covered several methods to retrieve the current date and time in Batch Script. From using simple environment variables to formatting and combining them for specific needs, these techniques will enhance your scripting skills and improve your automation tasks. Whether you’re logging events or creating dynamic file names, having the ability to manipulate date and time in Batch Script is invaluable. By implementing these examples, you can take your scripting to the next level and create more effective and organized scripts.
FAQ
-
How can I get the date in a different format?
You can manipulate the output of the%DATE%
variable using string operations in Batch Script to achieve your desired format. -
Can I use these methods in a scheduled task?
Yes, you can use these Batch Script techniques in scheduled tasks to log information or perform actions based on the current date and time. -
What if my system date format is different?
The output of%DATE%
can vary based on regional settings. You may need to adjust the parsing logic accordingly. -
Is it possible to save the date and time to a file?
Yes, you can redirect the output of your Batch Script to a file using the>
operator, allowing you to save the date and time. -
Can I use these commands in a command prompt directly?
Yes, you can run these commands directly in the command prompt for quick checks of the date and time.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn