Solved

Batch File: Fix date and time with Time Zone

Posted on 2014-01-10
7
1,347 Views
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,
Rene
0
Comment
Question by:ReneGe
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:CorinTack
ID: 39771522
Add the following to a batch file:

TZUTIL /s "Eastern Standard Time"
0
 
LVL 10

Author Comment

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

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

For example:

@ECHO OFF
CALL :TimeZone "2014-01-10 00:44:34"
ECHO %NewDate% %NewTime%
PAUSE
EXIT

:TimeZone
REM Code nere
EXIT /b
0
 
LVL 53

Accepted Solution

by:
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%
echo.
call :tzAdjust "2014-02-01 00:44:34" "-5"
echo Expected: 2014-01-31 19:44:34
echo tzAdjust: %tzAdjust%
echo.
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

~bp
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Author Comment

by:ReneGe
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

Cheers
0
 
LVL 53

Expert Comment

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

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 39772813
Whats wrong with me???

Thanks Bill :)
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39772866
Welcome Rene.

~bp
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…

856 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