Windows 10

Batch File To Get and Set System Date

In this tutorial, we’ll demonstrate how to retrieve and modify the system date using Windows batch scripting. These operations can be useful for automating tasks that depend on date conditions, logging, or maintaining time-based configurations.

A batch file contains a sequence of DOS (Disk Operating System) commands executed by the Windows Command Prompt. It’s a powerful tool for automating system-level tasks.
 

🔍 Retrieve the Current System Date

To fetch the current system date, use the %DATE% environment variable, which returns the system’s localized date format.
 
✅ Example:

@echo off
echo %DATE%

🧾 Sample Output:

Wed 10/06/2026
🧠 Note: The format of %DATE% depends on your system’s locale settings (e.g., MM/DD/YYYY, DD/MM/YYYY, or YYYY-MM-DD). You can parse specific portions (day, month, year) using substring manipulation if needed.

 
 

🛠 Set the System Date

You can change the system date using the date command. Administrator privileges are required to execute this command successfully.

⚠️ WARNING: Modifying the system date may affect time-sensitive applications, security certificates, scheduled tasks, and logs. Only change the system date in controlled environments (e.g., virtual machines, test systems).

✅ Example:

@echo off
date 15-02-2022

🔍 Explanation:

  • The command sets the system date to February 15, 2022.
  • The expected format (DD-MM-YYYY or MM-DD-YYYY) is determined by your system’s regional settings.

 

✅ Summary

Using batch scripting, you can easily retrieve or modify the system date via the %DATE% variable and date command. Just ensure you have the necessary permissions when setting the date and always validate locale formats to avoid errors.

Leave a Reply

Your email address will not be published. Required fields are marked *