You are not logged in.

#1 07 Aug 2006 19:10

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Display the current time in GMT (World Time)

This script gives the time in GMT

https://ss64.com/nt/syntax-gmt.html

Obviously just add or subtract a number from this to get the time in any other time zone

Offline

#2 28 Apr 2016 17:53

einst3
Member
From: Wethersfield, CT US
Registered: 28 Apr 2016
Posts: 2

Re: Display the current time in GMT (World Time)

What if the time offset is fractional like 3.5 hrs can someone fix the script to account for this?
can it tell if the offset is plus or minus?

Offline

#3 28 Apr 2016 18:32

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Display the current time in GMT (World Time)

einst3 wrote:

What if the time offset is fractional like 3.5 hrs can someone fix the script to account for this?
can it tell if the offset is plus or minus?

Yes, it will return a negative number if you're located to the east of the Prime Meridian.

Offline

#4 28 Apr 2016 23:35

einstein1969
Member
From: Italy
Registered: 29 Dec 2013
Posts: 25

Re: Display the current time in GMT (World Time)

It's correct?

  Local time: 00:28
  GMT time:   -2:28

Offline

#5 06 Feb 2018 20:26

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Display the current time in GMT (World Time)

Really it would be better to extend this so that it returns both data and time, you could then treat negative times differently - subtract 1 from the date and add 24 hours, so -2:28 would then become 22:28

Offline

#6 07 Feb 2018 06:33

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Display the current time in GMT (World Time)

The Win32_UTCTime WMI class already contains the GMT date and time, so frankly I don’t see the need for gmt.cmd?

[revoked example]

Last edited by Pyprohly (10 Feb 2018 09:32)

Offline

#7 08 Feb 2018 00:25

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Display the current time in GMT (World Time)

Because it wasn't available in 2006 when the topic was first created.

Offline

#8 09 Feb 2018 15:46

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Display the current time in GMT (World Time)

I have now updated the page https://ss64.com/nt/syntax-gmt.html to use wmic path Win32_UTCTime

In PowerShell, this can be done with just:

$mydtm = Get-Date            
$mydtm.ToUniversalTime()

Offline

#9 10 Feb 2018 09:31

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Display the current time in GMT (World Time)

Fair enough, Shadow Man.


Regarding the updated script, the need for that caret symbol in the FOR /F loops look questionable…

Offline

#10 10 Feb 2018 12:35

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Display the current time in GMT (World Time)

Pyprohly wrote:

Fair enough, Shadow Man.


Regarding the updated script, the need for that caret symbol in the FOR /F loops look questionable…

Try it without and see what happens!

consider why this fails:

@echo off 
setlocal EnableDelayedExpansion
for /f %%G in ('echo Hi ^& set _snark=Hello') do (echo [!_snark!])


C:\batch>demo.cmd
Environment variable _snark Hello not defined
[]

The important  thing here is that the variable _snark is not available in the DO clause, even with EnableDelayedExpansion, so the commands we run between the '  ' are running in a subshell, and thats why we need to escape the & and | characters if we want them to be ignored by the current shell and just passed to the subshell.

Offline

#11 11 Feb 2018 15:47

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Display the current time in GMT (World Time)

Oh, I didn’t even notice that caret symbol because it’s in such a routine position. I’m referring to the other caret symbol, the one in the options part: “delims=^=”.

The equals symbol doesn’t need escaping and the caret symbol isn’t escaping anything there.

Offline

#12 11 Feb 2018 23:22

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Display the current time in GMT (World Time)

Ah yes, good point - I'll fix that now.

Thanks

Offline

Board footer

Powered by