back to listing index

Creating a DateTime object with a specific UTC DateTime in PowerShell

[web search]
Original source (stackoverflow.com)
Tags: windows batch-script powershell timestamp utc-timestamp
Clipped on: 2016-11-17

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?

asked May 7 '12 at 18:17
Image (Asset 2/5) alt=
M. Dudley
11.9k25102191
up vote 16 down vote accepted

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()
answered May 7 '12 at 18:20
Image (Asset 3/5) alt=
M. Dudley
11.9k25102191
((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssfffffffZ")
answered Sep 11 '13 at 19:10
Image (Asset 5/5) alt=
Ryker Abel
14122
2 upvote
  flag
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 – Anders Rask Jan 12 '15 at 14:43
    
This works great! PowerShell -NoProfile -ExecutionPolicy Bypass -Command "((get-date).ToUniversalTime()).ToString(\"yyyy-MM-ddTHH:mm:‌​ss+0000\")" – Jay Taylor 9 secs ago   edit  

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. 
answered May 7 '12 at 20:46
Shay Levy
63k1097122

Your Answer

asked

4 years ago

viewed

30062 times

active

1 year ago

Upcoming Events

Blog

Featured on Meta

Get the weekly newsletter! In it, you'll get:

  • The week's top questions and answers
  • Important community announcements
  • Questions that need answers
Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Software Engineering
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. Cryptography
  1. Code Review
  2. Magento
  3. Signal Processing
  4. Raspberry Pi
  5. Programming Puzzles & Code Golf
  6. more (7)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Music: Practice & Theory
  6. Seasoned Advice (cooking)
  7. Home Improvement
  8. Personal Finance & Money
  1. Academia
  2. more (8)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. English Language Learners
  7. Japanese Language
  8. Arqade (gaming)
  1. Bicycles
  2. Role-playing Games
  3. Anime & Manga
  4. Motor Vehicle Maintenance & Repair
  5. more (17)
  1. MathOverflow
  2. Mathematics
  3. Cross Validated (stats)
  4. Theoretical Computer Science
  5. Physics
  6. Chemistry
  7. Biology
  8. Computer Science
  1. Philosophy
  2. more (3)
  1. Meta Stack Exchange
  2. Stack Apps
  3. Area 51
  4. Stack Overflow Talent
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.11.17.4214