Angel Eyes
Angel Eyes

Reputation: 302

Get datestamp for a different timezone (Windows command line)

I need to get the datestamp for 7 hrs in the future.

I currently have a solution but it requires changing the system date, which is not ideal. Does anybody know how to do this without changing the time zone?

Current Method (my time zone is PST and azores is +7hrs):

tzutil /s "Azores Standard Time"

echo %DATE:~-4%%DATE:~4,2%%DATE:~7,2%

tzutil /s "Pacific Standard Time"

Upvotes: 0

Views: 5132

Answers (2)

dbenham
dbenham

Reputation: 130859

This is very easy using getTimeStamp.bat - a hybrid JScript/batch utility that does timestamp computations and formatting The utility is pure script that will run on any modern Windows machine from XP onward - no 3rd party executable required.

Full documentation is built into the script. It has oodles of options to specify a given timestamp (or use current local date/time), add date and time offsets, specify time zones, and format the result.

It looks to me like you want your date to have YYYYMMDD format. Assuming you have getTimeStamp.bat in your current directory, or better yet, somewhere within your path, then the following command issued from the command prompt will print out the date 7 hours in the future:

getTimeStamp -oh +7 -f {yyyy}{mm}{dd}

To use it in a batch file and store the result in the dt variable

call getTimeStamp -oh +7 -f {yyyy}{mm}{dd} -r dt

If you know the time zone you want, relative to GMT, then you can specify the timezone offset instead of the offset from local time. I believe Azores is GMT - 60 min, so you could use:

call getTimeStamp -z -60 -f {yyyy}{mm}{dd} -r dt

Upvotes: 3

foxidrive
foxidrive

Reputation: 41244

This works with a VBS script in a batch file.

  @echo off
  set TmpFile="%temp%.\tmp.vbs"
  echo> %TmpFile% n=Now
  echo>>%TmpFile% s=DateAdd("h", 7, n)
  echo>>%TmpFile% Wscript.Echo "set plus7=" ^& s
  cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
  call "%temp%.\tmp.bat"
  del  "%temp%.\tmp.bat"
  del  %TmpFile%
  set TmpFile=
  set "stamp=%plus7:~6,4%-%plus7:~3,2%-%plus7:~0,2%.%plus7:~11,2%_%plus7:~14,2%_%plus7:~17,2%"

  echo "%plus7%"  
  echo "%stamp%"  


  pause

Upvotes: 1

Related Questions