Batch File: Fix date and time with Time Zone

Posted on 2014-01-10
Last Modified: 2014-01-10
Hi there,

I have a date, and a time at 0GMT.

To need a batch file to change them according to -5GMT

For example:
2014-01-10 00:44:34  -> 2014-01-09 19:44:34
2014-02-01 00:44:34 -> 2014-01-31 19:44:34
2014-02-01 20:44:34 -> 2014-02-01 15:44:34

Thanks for your help,
Question by:ReneGe
  • 3
  • 3

Expert Comment

ID: 39771522
Add the following to a batch file:

TZUTIL /s "Eastern Standard Time"
LVL 10

Author Comment

ID: 39771552
hey CorinTack, thank for your prompt reply.

I don't need to change the time zone of my PC.

For example:

CALL :TimeZone "2014-01-10 00:44:34"
ECHO %NewDate% %NewTime%

REM Code nere
LVL 52

Accepted Solution

Bill Prew earned 500 total points
ID: 39772103
You might think of this as "cheating" but it's a very useful approach in BAT scripts.

@echo off
REM Initialization
echo d=DateAdd("h",CInt(Wscript.Arguments.Item(1)),CDate(Wscript.Arguments.Item(0)))>"%TEMP%\_t.vbs"
echo Wscript.Echo Year(d)^&"-"^&Right("0"^&Month(d),2)^&"-"^&Right("0"^&Day(d),2)^&" "^&Right("0"^&Hour(d),2)^&":"^&Right("0"^&Minute(d),2)^&":"^&Right("0"^&Second(d),2)>>"%TEMP%\_t.vbs"

REM Main logic
call :tzAdjust "2014-01-10 00:44:34" "-5"
echo Expected: 2014-01-09 19:44:34
echo tzAdjust: %tzAdjust%
call :tzAdjust "2014-02-01 00:44:34" "-5"
echo Expected: 2014-01-31 19:44:34
echo tzAdjust: %tzAdjust%
call :tzAdjust "2014-02-01 20:44:34" "-5"
echo Expected: 2014-02-01 15:44:34
echo tzAdjust: %tzAdjust%

REM Cleanup
del "%TEMP%\_t.vbs"
exit /b

:tzAdjust [date-time] [hours-offset]
  for /F "tokens=*" %%A in ('cscript //nologo "%TEMP%\_t.vbs" "%~1" "%~2"') do set tzAdjust=%%A
  exit /b

Open in new window

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 10

Author Comment

ID: 39772207
Hey buddy, its been a while :)

here is my output:
Expected: 2014-01-09 19:44:34
tzAdjust: 2014-01-09 19:44:34

Expected: 2014-01-31 19:44:34
tzAdjust: 2014-01-31 19:44:34

Expected: 2014-02-01 15:44:34
tzAdjust: 2014-02-01 15:44:34

LVL 52

Expert Comment

by:Bill Prew
ID: 39772746
Correct, that's the proper output, as you requested.

LVL 10

Author Comment

ID: 39772813
Whats wrong with me???

Thanks Bill :)
LVL 52

Expert Comment

by:Bill Prew
ID: 39772866
Welcome Rene.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to split this type of line? 5 83
how to find then copy to anotehr file 3 45
C# DataGridView_RowsAdded event not firing 1 64
Basic Java Case or If-Else statement... 3 43
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now