Batch Script - DATE and TIME

Hello, aspiring programmers! Today, we're going to dive into the fascinating world of dates and times in Batch scripting. As your friendly neighborhood computer teacher, I'm excited to guide you through this journey. Don't worry if you've never written a line of code before – we'll start from the basics and work our way up. So, grab your virtual notepads, and let's get started!

Batch Script - DATE & TIME

DATE Command

The DATE command in Batch scripting is like your digital calendar. It allows you to view or change the current date on your computer. Let's explore how to use it!

Viewing the Current Date

To view the current date, simply type:

DATE

When you run this command, you'll see something like:

The current date is: Mon 04/17/2023
Enter the new date: (mm-dd-yy)

Here's a fun fact: I once had a student who used this command to check if it was his birthday. He was so excited when it actually was!

Changing the Date

To change the date, you can enter a new date when prompted. For example:

DATE 04-18-2023

This would change the date to April 18, 2023. But be careful! Changing your system date can affect various applications and processes.

Using DATE in Scripts

Now, let's see how we can use the DATE command in our scripts. Here's a simple example:

@echo off
echo Today's date is:
date /t
pause

In this script:

  1. @echo off prevents the commands from being displayed.
  2. echo Today's date is: prints a message.
  3. date /t displays the current date without prompting for a new date.
  4. pause keeps the window open so you can see the result.

TIME Command

Just like DATE, the TIME command lets you view or change the current time on your computer. It's like having a digital watch at your fingertips!

Viewing the Current Time

To view the current time, simply type:

TIME

You'll see something like:

The current time is: 14:30:45.52
Enter the new time:

Changing the Time

To change the time, enter a new time when prompted:

TIME 15:45

This would change the time to 3:45 PM.

Using TIME in Scripts

Let's create a script that displays both the date and time:

@echo off
echo Current date and time:
date /t
time /t
pause

This script will show you the current date and time without prompting for changes.

Date in Format Year-Month-Day

Sometimes, you might need to display the date in a specific format, like Year-Month-Day. Batch scripting doesn't have a built-in command for this, but we can create our own solution using environment variables.

Here's a script that displays the date in YYYY-MM-DD format:

@echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set datetime=%%I
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
echo Current date: %year%-%month%-%day%
pause

Let's break this down:

  1. The for loop runs the wmic command to get the local date and time.
  2. We extract the date information into variables: year, month, and day.
  3. Finally, we echo the date in the desired format.

This script might look a bit complex, but it's really powerful. I remember a student who used this to automatically date-stamp his homework files!

Useful Date and Time Methods

Here's a table of some useful date and time methods in Batch scripting:

Method Description Example
DATE /T Display current date DATE /T
TIME /T Display current time TIME /T
%DATE% Environment variable for current date echo %DATE%
%TIME% Environment variable for current time echo %TIME%
wmic os get localdatetime Get date and time in YYYYMMDDHHMMSS format wmic os get localdatetime

Remember, practice makes perfect! Try writing scripts that use these commands in different ways. Maybe create a digital clock, or a script that calculates how many days until your next birthday.

In conclusion, working with dates and times in Batch scripting opens up a world of possibilities. From simple date displays to complex time calculations, these tools can be incredibly useful in your programming journey. Keep experimenting, and don't be afraid to make mistakes – that's how we learn!

Happy scripting, future programmers!

Credits: Image by storyset