Solved

Batch File: Fix date and time with Time Zone

Posted on 2014-01-10
7
1,313 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
file size 9 51
Need to know a list of new AD users that are created during the day. 5 46
Command prompt output to variable? 9 44
Batch Files - Error Handling 4 25
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

785 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