DOS Batch script - Variable for current system time plus 2 minutes?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ClintBarton
Posts: 1
Joined: 18 Jun 2018 07:20

DOS Batch script - Variable for current system time plus 2 minutes?

#1 Post by ClintBarton » 20 Jun 2018 22:27

Hi,

I am limited to batch scripting for one particular task we do and in it I need to schedule a task. I'm using schtasks to create the task and it works great except for the start time. This part of the script runs whenever the previous sections have completed and the time it takes changes everyday so I can't enter a time that will work all the time. I would like to just create a variable that is 2 minutes ahead of the system time but I don't know how to do this?

Please help.

I didn't find the right solution from the Internet.

References:-
https://arstechnica.com/civis/viewtopic.php?t=390290

Thanks!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: DOS Batch script - Variable for current system time plus 2 minutes?

#2 Post by Squashman » 21 Jun 2018 00:08

Didn't look to hard at the link you posted but these days I will usually call out to Powershell within my batch file to do any type of date and time math.

Code: Select all

FOR /F "delims=" %%G IN ('powershell "(Get-Date).AddMinutes(2).ToString('yyyyMMdd hh:mm')"') DO set dtstamp=%%G
If you want to get all the individual date and time segments into their own variables you could do this.

Code: Select all

FOR /F "TOKENS=1-5 delims= " %%G IN ('powershell "(Get-Date).AddMinutes(2).ToString('yyyy MM dd hh mm')"') DO (
	set year=%%G
	set month=%%H
	set day=%%I
	set hour=%%J
	set second=%%K
)	

capella
Posts: 6
Joined: 21 Jun 2018 07:45

Re: DOS Batch script - Variable for current system time plus 2 minutes?

#3 Post by capella » 21 Jun 2018 09:15

Below is a batch script I put together when I was learning how to work with dates and times for a project. It breaks down all of the parts of the date and time and shows how the parts can be put in variables and worked with in various ways.

Code: Select all

@echo off
@setlocal enabledelayedexpansion

echo.
echo System date: %DATE%

REM Echo the parts of the date
echo.
echo Month: %DATE:~4,2%

echo.
echo Date: %DATE:~7,2%

echo.
echo Year (2 digits): %DATE:~12,2%
echo Year (4 digits): %DATE:~10,4%

SET _CURRDATE=%DATE:~4,2%/%DATE:~7,2%/%DATE:~12,2%

echo.
echo Formatted date: %_CURRDATE%

echo.
echo System time: %TIME%

REM Echo the parts of the time
echo.
echo Hour: %TIME:~0,2%

REM Put the hour into a variable to make it easier to work with
SET _HOUR=%TIME:~0,2%

echo.
echo Unformatted hour: %_HOUR%

REM Add leading zero IF hour is a single digit
SET _HOUR=%TIME:~0,2%
IF "%_HOUR:~0,1%" == " " SET _HOUR=0%_HOUR:~1,1%

echo.
echo Formatted hour: %_HOUR%

echo.
echo Minutes: %TIME:~3,2%

REM Put the minutes in a variable too
SET _MINUTES=%TIME:~3,2%

echo.
echo Seconds: %TIME:~6,2%

REM Put the seconds into a variable
SET _SECONDS=%TIME:~6,2%

REM Finally, the hundredths of a second:
echo.
echo Hundredths of a second: %TIME:~9,2%

SET _HUNDREDTHS=%TIME:~9,2%

REM Put together a timestamp
SET _TIMESTAMP=%_HOUR%:%_MINUTES%

echo.
echo Timestamp: %_TIMESTAMP%

REM Put it all together
echo.
echo The time is: %_TIMESTAMP%. And the date is: %_CURRDATE%
echo.
echo Detailed time: %_TIMESTAMP%:%_SECONDS%.%_HUNDREDTHS%

echo.

@pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: DOS Batch script - Variable for current system time plus 2 minutes?

#4 Post by Squashman » 21 Jun 2018 09:20

capella wrote:
21 Jun 2018 09:15
Below is a batch script I put together when I was learning how to work with dates and times for a project. It breaks down all of the parts of the date and time and shows how the parts can be put in variables and worked with in various ways.

Code: Select all

@echo off
@setlocal enabledelayedexpansion

echo.
echo System date: %DATE%

REM Echo the parts of the date
echo.
echo Month: %DATE:~4,2%

echo.
echo Date: %DATE:~7,2%

echo.
echo Year (2 digits): %DATE:~12,2%
echo Year (4 digits): %DATE:~10,4%

SET _CURRDATE=%DATE:~4,2%/%DATE:~7,2%/%DATE:~12,2%

echo.
echo Formatted date: %_CURRDATE%

echo.
echo System time: %TIME%

REM Echo the parts of the time
echo.
echo Hour: %TIME:~0,2%

REM Put the hour into a variable to make it easier to work with
SET _HOUR=%TIME:~0,2%

echo.
echo Unformatted hour: %_HOUR%

REM Add leading zero IF hour is a single digit
SET _HOUR=%TIME:~0,2%
IF "%_HOUR:~0,1%" == " " SET _HOUR=0%_HOUR:~1,1%

echo.
echo Formatted hour: %_HOUR%

echo.
echo Minutes: %TIME:~3,2%

REM Put the minutes in a variable too
SET _MINUTES=%TIME:~3,2%

echo.
echo Seconds: %TIME:~6,2%

REM Put the seconds into a variable
SET _SECONDS=%TIME:~6,2%

REM Finally, the hundredths of a second:
echo.
echo Hundredths of a second: %TIME:~9,2%

SET _HUNDREDTHS=%TIME:~9,2%

REM Put together a timestamp
SET _TIMESTAMP=%_HOUR%:%_MINUTES%

echo.
echo Timestamp: %_TIMESTAMP%

REM Put it all together
echo.
echo The time is: %_TIMESTAMP%. And the date is: %_CURRDATE%
echo.
echo Detailed time: %_TIMESTAMP%:%_SECONDS%.%_HUNDREDTHS%

echo.

@pause
Your code is region dependent. Your date and time variables may not output the same as mine depending on what country you live in. The best options for a region independent solution is to use WMIC or Powershell within a FOR /F command to capture the output in the same consistent format every time regardless of what computer it runs on.

capella
Posts: 6
Joined: 21 Jun 2018 07:45

Re: DOS Batch script - Variable for current system time plus 2 minutes?

#5 Post by capella » 21 Jun 2018 09:29

Thank you for the clarification, Squashman. I admit I'm terribly new to batch scripting, so I probably should have divulged that information!

You're right, as a VB.NET programmer, I understand the need to keep localization in mind.

Post Reply