51

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?

7 Answers 7

55

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()
Sign up to request clarification or add additional context in comments.

3 Comments

disproof: $UtcTime.Kind I mean, when you pass it to some other function - in most cases it will treat it as a local time, because this is what is actually stored. And PowerShell does not converts it to your local time when prints... Otherwise your ToUniversalTime() also would be local
Get-Date converts the given UTC time to local time and stores it as a datetime object with Kind set to Local. It does store the correct physical point in time, but it is a local time and will be printed in local time. Note that this still IS a correct answer to the question. OP did not specifically ask for creating a datetime object with kind UTC. Upvoted.
1/1/70 12:00:00 AM Why does it not show the year?
27
(get-date).ToUniversalTime().ToString("yyyyMMddTHHmmssfffffffZ")

3 Comments

The ToString value should support 24 hours, so it should have capital HH: .ToString("yyyyMMddTHHmmssfffffffZ") eg. 02:00:00PM should become ..140000 since UTC is timezone-agnostic
This works great! PowerShell -NoProfile -ExecutionPolicy Bypass -Command "((get-date).ToUniversalTime()).ToString(\"yyyy-MM-ddTHH:mm:ss+0000\")"
Thank you. Finding this was surprisingly time-consuming.
20
$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.

Comments

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

This appears to also work

1 Comment

[DateTime]::UtcNow.ToString('u') is a better choice as those MM and mm might be misspelled. I filed such a bug in a system the other month.
5

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

Comments

0

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. 

Comments

0

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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.