Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell simplest method to get current time expressed as UTC

I have reviewed the post Creating a DateTime object with a specific UTC DateTime in PowerShell, but it does not seem to directly answer the question I am asking:

What is the most direct method in PowerShell (3.0) to return a sortable string representing "now" as UTC?

I expected the correct answer to be:

Get-Date -Format (Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern 

 OR

get-date -format u

but this is not the case.

Example: At 1300 hrs (1pm) on September 1st, 2016 in the Pacific Time Zone during DST, I get the response:

2016-09-01 13:00:00Z (the local time with a "Z" appended)

when I was expecting:

2016-09-01 20:00:00Z (correct UTC/GMT time)

So basically, my code is just getting a string representing the "local" time and appending a "Z".

Now, I know I can manipulate to get to that point, but I'm looking for the minimal (simplest, cleanest) way to get here.

Bonus Points (as if they existed): How do I get that same, sortable result, but displaying with "UTC" and/or "GMT" as the suffix. Same minimal requirement.

like image 816
dave_the_dev Avatar asked Sep 01 '15 20:09

dave_the_dev


People also ask

How do you represent time in UTC?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

How do I get the current time in PowerShell?

Get-Date uses the UFormat parameter with format specifiers to display the current system date and time. The format specifier %Z represents the UTC offset of -07. The $Time variable stores the current system date and time. $Time uses the ToUniversalTime() method to convert the time based on the computer's UTC offset.

How do I set DateTime to UTC?

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.

How do I know if DateTime is UTC?

DateTime dt = (DateTime) dataReader["YourDataField"]; DateTime utc = DateTime. SpecifyKind(dt, DateTimeKind. Utc); Instant instant = Instant. FromDateTimeUtc(utc);


1 Answers

Probably something like this:

[DateTime]::UtcNow.ToString('u')

Which is equivalent to:

[DateTime]::UtcNow.ToString((Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern)

For the bonus, I think the most straightforward way is just to replace Z with UTC:

[DateTime]::UtcNow.ToString('u').Replace('Z','UTC')

I'm assuming you'll always want UTC since that what it seems like from your question. There doesn't appear to be a format string to get just the 3 letter time zone.

like image 107
briantist Avatar answered Nov 15 '22 19:11

briantist