Tuesday, 10 August 2010

Convert-Date.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script manipulates a time using TimeZone info methods. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell. The script first 
  6.     creates a DateTime object then it converts it to Universal, UTC, Pacific and 'local' time.   
  7. .NOTES 
  8.     File Name  : Convert-Date.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/08/convert-dateps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.TimeZoneINfo\Convert-date.ps 
  18.     Date           : 3/21/2006 02:00:00 AM 
  19.     Local Time zone: GMT Standard Time 
  20.      In Universal time: 3/21/2006 02:00:00 AM 
  21.      In UTC           : 3/21/2006 02:00:00 AM 
  22.      In Local TZ:     : 3/21/2006 02:00:00 AM 
  23.      In Pacific TZ    : 3/21/2006 10:00:00 AM 
  24. #> 
  25. # Create date object 
  26. $date1 = New-Object System.DateTime 2006, 3, 21, 2, 0, 0 
  27.  
  28. # Display date and local time zone then show that time in other time zones 
  29. "Date           : {0}" -f $date1 
  30. "Local Time zone: {0}" -f  ([System.TimeZoneInfo]::Local).id 
  31. " In Universal time: {0}" -f $date1.ToUniversalTime() 
  32. " In UTC           : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1
  33. " In Local TZ:     : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, ([System.TimeZoneInfo]::Local)) 
  34. $tz = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time");   
  35. " In Pacific TZ    : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, $tz

No comments: