sourcenouveau
sourcenouveau

Reputation: 30554

Creating a DateTime object with a specific UTC DateTime in PowerShell

I'm trying to create a DateTime object with a specific UTC timestamp in PowerShell. What's the simplest way to do this?

I tried:

Get-Date
    -Format (Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern
    -Date "1970-01-01 00:00:00Z"

but I get this output:

1969-12-31 19:00:00Z

It's a few hours off. Where's my lapse in understanding?

Upvotes: 51

Views: 131597

Answers (7)

BenCep
BenCep

Reputation: 1

Reference

In Powershell, the method

New-Object DateTime(9999, 12, 31, 23, 59, 59, 59, ([DateTimeKind]::Utc))

is the correct one, as the others create the time in Local TZ, and then convert it and store it in UTC and then it can be displayed again in UTC, whereas the above method creates it in UTC in the first place.

You can test this by reaching the .NET DateTime limits like the above, whereas if you run the script from the "accepted solution" at the limits

(Get-Date -Date "9999-12-31T23:59:59.99Z").ToUniversalTime()

run from a system with a local time zone ahead of UTC, e.g. UTC+1, then you'll receive the below error:

Get-Date: Cannot bind parameter 'Date'. Cannot convert value "9999-12-31T23:59:59.99Z" to type "System.DateTime". Error: "The DateTime represented by the string '9999-12-31T23:59:59.99Z' is out of range."

Upvotes: 0

hotch
hotch

Reputation: 51

You can use the SpecifyKind method:

PS C:\IT\s3> $timestamp

Wednesday, July 18, 2018 7:57:14 PM

PS C:\IT\s3> $timestamp.kind

Unspecified

PS C:\IT\s3> $utctimestamp = [DateTime]::SpecifyKind($timestamp,[DateTimeKind]::Utc)

PS C:\IT\s3> $utctimestamp

Wednesday, July 18, 2018 7:57:14 PM

PS C:\IT\s3> $utctimestamp.kind

Utc

Upvotes: 5

user8691702
user8691702

Reputation: 81

$time = [DateTime]::UtcNow | get-date -Format "yyyy-MM-ddTHH:mm:ssZ"

This appears to also work

Upvotes: 8

Florian Winter
Florian Winter

Reputation: 5329

$utctime = New-Object DateTime 1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc)

If you print out $utctime, then you get:

1. januar 1970 00:00:00

Also, $utctime.Kind is correctly set to Utc.

Upvotes: 19

Ryker Abel
Ryker Abel

Reputation: 639

(get-date).ToUniversalTime().ToString("yyyyMMddTHHmmssfffffffZ")

Upvotes: 27

Shay Levy
Shay Levy

Reputation: 126932

This is how it works in .NET, right? PowerShell just calls the ToUniversalTime method. From http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx

The Coordinated Universal Time (UTC) is equal to the local time minus the 
UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset.
The conversion also takes into account the daylight saving time rule that applies 
to the time represented by the current DateTime object. 

Upvotes: 0

sourcenouveau
sourcenouveau

Reputation: 30554

The DateTime object itself is being created with the proper UTC time. But when PowerShell prints it out it converts it to my local culture and time zone, thus the difference.

Proof:

$UtcTime = Get-Date -Date "1970-01-01 00:00:00Z"
$UtcTime.ToUniversalTime()

Upvotes: 55

Related Questions