Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/10487011/creating-a-datetime-object-with-a-specific-utc-datetime-in-powershell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...