Format date and time in a Windows batch script

Cover Image for Format date and time in a Windows batch script
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Formatting Date and Time in a Windows Batch Script

So you're working with a Windows batch script and you need to format the current date and time for later use in file names and other purposes. You want to know how to include the time in the format, and specifically how to handle single-digit hours. Don't worry, we've got you covered!

The Initial Approach

Before diving into the solution, let's take a look at the initial code snippet provided:

echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%

This code gives the following output:

28/07/2009
 8:35:31.01
2009_07_28__ 8_36_01

As you can see, the date and time are extracted from the system variables %DATE% and %TIME% respectively. However, there is an issue with the format of the time portion. Let's proceed to the solution.

Handling Single-Digit Hours

To handle single-digit hours, we need to modify the code slightly. Instead of using %time:~0,2% to extract the hour portion, we'll use the following approach:

IF "%TIME:~0,1%"==" " (
    set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%_0%time:~1,1%_%time:~3,2%_%time:~6,2%
) ELSE (
    set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
)

This updated code snippet checks if the first character of %TIME% (which represents the hour) is a space. If it is, it means that the hour is a single digit. In this case, we prepend a 0 to the hour portion using 0%time:~1,1%. If the hour is already two digits, the original code continues to be used.

Running the modified code will give you the desired output:

2009_07_28__08_36_01

Wrap Up and Engage!

And there you have it! You now know how to format the date and time in a Windows batch script while handling single-digit hours. Feel free to use this technique to incorporate the formatted date and time into your file names, log entries, or any other part of your script.

Was this solution helpful? Do you have any other questions or cool batch script tricks to share? Let us know in the comments below! Keep learning and keep coding! 💻✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello