Showing posts with label system.datetime. Show all posts
Showing posts with label system.datetime. Show all posts

Tuesday, 8 November 2011

Show-TimeSpan.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script, a re-implementation of an MSDN sample, shows 
  4.     details of a timespan object 
  5. .DESCRIPTION 
  6.     This script re-implements a simple MSDN script that creates a  
  7.     timespan object and displays its properties. 
  8. .NOTES 
  9.     File Name  : Show-TimeSpan.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/system.timespan.aspx 
  17. .EXAMPLE 
  18.      Psh> .\Show-TimeSpan.ps1 
  19.     8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15 
  20.        Value of Days Component:                             229 
  21.        Total Number of Days:                   229.229340277778 
  22.        Value of Hours Component:                              5 
  23.        Total Number of Hours:                  5501.50416666667 
  24.        Value of Minutes Component:                           30 
  25.        Total Number of Minutes:                       330090.25 
  26.        Value of Seconds Component:                           15 
  27.        Total Number of Seconds:                      19,805,415 
  28.        Value of Milliseconds Component:                       0 
  29.        Total Number of Milliseconds:             19,805,415,000 
  30.        Ticks:                               198,054,150,000,000 
  31. #> 
  32.  
  33. # Define two dates 
  34. $date1 = new-object system.datetime 2010, 1, 1, 8, 0, 15 
  35. $date2 = new-object system.datetime 2010, 8, 18, 13, 30, 30 
  36.  
  37. # Create a time Interval 
  38. $interval = New-Timespan -start $date1 -end $date2 
  39.  
  40. #Display the interval 
  41. "{0} - {1} = {2}" -f $date2, $date1, $interval.ToString() 
  42.  
  43. # Display individual properties of the resulting TimeSpan object. 
  44. "   {0,-35} {1,20}" -f "Value of Days Component:", $interval.Days 
  45. "   {0,-35} {1,20}" -f "Total Number of Days:", $interval.TotalDays 
  46. "   {0,-35} {1,20}" -f "Value of Hours Component:", $interval.Hours 
  47. "   {0,-35} {1,20}" -f "Total Number of Hours:", $interval.TotalHours 
  48. "   {0,-35} {1,20}" -f "Value of Minutes Component:", $interval.Minutes 
  49. "   {0,-35} {1,20}" -f "Total Number of Minutes:", $interval.TotalMinutes 
  50. "   {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $interval.Seconds 
  51. "   {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $interval.TotalSeconds 
  52. "   {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $interval.Milliseconds 
  53. "   {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $interval.TotalMilliseconds 
  54. "   {0,-35} {1,20:N0}" -f "Ticks:", $interval.Ticks 

Monday, 2 August 2010

Get-ConvertedTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script converts time using ConvertTime method.  
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample. 
  6. .NOTES 
  7.     File Name  : Get-ConvertedTime.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/08/get-convertedtimeps1.html
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/bb382835.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-ConvertedTime.ps1 
  17.     Local time zone: (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London 
  18.  
  19.     Converted 1/1/2010 12:01:00 AM Unspecified to 12/31/2009 07:01:00 PM 
  20.     Converted 1/1/2010 12:01:00 AM Utc to 12/31/2009 07:01:00 PM 
  21.     Converted 1/1/2010 12:01:00 AM Local to 12/31/2009 07:01:00 PM 
  22.     Converted 11/6/2010 11:30:00 PM Unspecified to 11/6/2010 07:30:00 PM 
  23.     Converted 11/7/2010 02:30:00 AM Unspecified to 11/6/2010 10:30:00 PM 
  24. #> 
  25. #Create an array of times to convert 
  26. [DateTime[]] $times = (New-Object system.dateTime 2010, 1, 1, 0, 1, 0),  
  27.                       (New-Object system.DateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Utc)),  
  28.                       (New-Object system.dateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Local)),                             
  29.                       (New-Object system.DateTime(2010, 11, 6, 23, 30, 0)), 
  30.                       (New-Object system.DateTime(2010, 11, 7, 2, 30, 0) ); 
  31.  
  32. # Retrieve the time zone for Eastern Standard Time (U.S. and Canada). 
  33. try { 
  34.   $Est = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time"); 
  35. catch { 
  36.   "Unable to retrieve the Eastern Standard time zone." 
  37.   return
  38.  
  39. #  Display the current time zone name. 
  40. "Local time zone: {0}`n" -f [system.TimeZoneInfo]::Local.DisplayName 
  41.  
  42. # Convert and display each time in the $times array 
  43.  foreach ($timeToConvert in $times)  { 
  44.   $TargetTime = [System.TimeZoneInfo]::ConvertTime($TimeToConvert, $Est
  45.   "Converted {0} {1} to {2}" -f $timeToConvert,$timeToConvert.Kind, $targetTime 
  46. }                         

Wednesday, 5 August 2009

Get-Thursday

  1. <# 
  2. .SYNOPSIS 
  3.     This script checks of a particular date in the past was a Thurday. 
  4. .DESCRIPTION 
  5.     This script creates a DateTime object set for 1st May, 2003. The 
  6.     script then check to see if that day is a Thursday then displays
  7.     the day of week for that date (which is a Thursday). This script
  8.     is a copy of the MSDN sample, written in PowerShell.   
  9. .NOTES 
  10.     File Name  : Get-Thursday.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/08/get-thursday.html
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.dayofweek(VS.71).aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.DateTime\get-thursday.ps1' 
  20.     Is Thursday the day of the week for 5/1/2003?: True 
  21.     The day of the week for 5/1/2003 is Thursday. 
  22. #> 
  23.   
  24. ## 
  25. # Start of script 
  26. ## 
  27.   
  28. # Create a DateTime for the first of May, 2003. 
  29. $dt = New-Object System.DateTime 2003, 5, 1 
  30.   
  31. # Now - is it Thursday? 
  32. "Is Thursday the day of the week for {0:d}?: {1}" -f $dt,($dt.DayOfWeek -eq [system.DayOfWeek]::Thursday) 
  33. "The day of the week for {0:d} is {1}." -f $dt, $dt.DayOfWeek 
  34. # End of Script 

Thursday, 12 March 2009

Get-DaysInMonth.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Displys number of days in a particular month.   
  4. .DESCRIPTION 
  5.     This scrips uses System.DateTime to determine, then 
  6.     display, the numbe of days in some months. 
  7. .NOTES 
  8.     File Name  : Get-DaysInMonth.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     PSH [C:\foo]: . 'C:\foo\Get-daysinmonth.ps1' 
  15.     There are 31 days in July 2001 
  16.     There are 28 days in February 1998 
  17.     There are 29 days in February 1996 
  18.     There are 31 days in August 1950 
  19. #> 
  20.  
  21. ## 
  22. # Start of script 
  23. ## 
  24.  
  25. # Create variables representing months 
  26. $July      = 7 
  27. $February  = 2 
  28. $August    = 8 
  29.  
  30. # daysInJuly should be 31. 
  31. $daysInJuly = [System.DateTime]::DaysInMonth(2001, $July
  32. "There are $daysinjuly days in July 2001" 
  33.  
  34. # DaysInFeb should be 28 because the year 1998 was not a leap year. 
  35. $daysInFeb = [System.DateTime]::DaysInMonth(1998, $February
  36. "There are $daysinfeb days in February 1998" 
  37.  
  38. # daysInFebLeap gets 29 because the year 1996 was a leap year. 
  39. $daysInFebLeap = [System.DateTime]::DaysInMonth(1996, $February
  40. "There are $daysinfebleap days in February 1996" 
  41.  
  42. # An august year indeed. 
  43. $daysInAugust = [System.DateTime]::DaysInMonth(1950, $August
  44. "There are $daysinAugust days in August 1950" 
  45. # End of Script 

Monday, 19 January 2009

Get-LeapYear.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates use of System.Datetime to determine leap year  
  4. .DESCRIPTION 
  5.     This script creates four date objects and checks to see if 
  6.     the date is a leap year. The last object is today's date.  
  7. .NOTES 
  8.     File Name  : Get-LeapYear.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     PS c:\foo> .\Get-LeapYear.ps1 
  15.     2000 is a leap year: True 
  16.     2002 is a leap year: False 
  17.     2004 is a leap year: True 
  18.     2009 is a leap year: False 
  19. #> 
  20.  
  21. ## 
  22. #  Start of script 
  23. ## 
  24.  
  25. # Create three specific date objects, plus today 
  26. $d1 = [system.datetime]  "Jan 1 2000" 
  27. $d2 = [System.datetime]  "Jan 1 2002" 
  28. $d3 = [System.datetime]  "Jan 1 2004" 
  29. $d4 = get-date 
  30.  
  31. # Are they leap years?
  32. "{0} is a leap year: {1}" -f $d1.year,([system.datetime]::isleapyear($d1.year)) 
  33. "{0} is a leap year: {1}" -f $d2.year,([system.datetime]::isleapyear($d2.year)) 
  34. "{0} is a leap year: {1}" -f $d3.year,([system.datetime]::isleapyear($d3.year)) 
  35. "{0} is a leap year: {1}" -f $d4.year,([system.datetime]::isleapyear($d4.year)) 
  36. # End Script 

Friday, 2 January 2009

Get-DateTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.   Uses .NET formatting strings to format date/time values   
  4. .DESCRIPTION 
  5.   Recreates an MSDN sample for displaying Date/TIme Format. See 
  6.   links section for pointer to original Sample. 
  7. .NOTES 
  8.     File Name  : Get-DateTime.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .EXAMPLE 
  12.     PS C:\foo> .\Get-DateTime.ps1 
  13.     FORMAT  en-gb EXAMPLE 
  14.     CHAR    VALUE OF ASSOCIATED PROPERTY, if ANY 
  15.       d     12/28/2008 
  16.             dd/MM/yyyy (ShortDatePattern) 
  17.       D     28 December 2008 
  18.             dd MMMM yyyy (LongDatePattern) 
  19.       f     28 December 2008 16:37 
  20.       F     28 December 2008 16:37:00 
  21.             dd MMMM yyyy HH:mm:ss (FullDateTimePattern) 
  22.       g     28/12/2008 16:37 
  23.       G     28/12/2008 16:37:00 
  24.       m     28 December 
  25.             dd MMMM (MonthDayPattern) 
  26.       M     28 December 
  27.             dd MMMM (MonthDayPattern) 
  28.       o     2008-12-28T16:37:00.4017900+00:00 
  29.       r     Sun, 28 Dec 2008 16:37:00 GMT 
  30.             ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern) 
  31.       R     Sun, 28 Dec 2008 16:37:00 GMT 
  32.             ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern) 
  33.       s     2008-12-28T16:37:00 
  34.             yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern) 
  35.       t     16:37 
  36.             HH:mm (ShortTimePattern) 
  37.       T     16:37:00 
  38.             HH:mm:ss (LongTimePattern) 
  39.       u     2008-12-28 16:37:00Z 
  40.             yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern) 
  41.       U     28 December 2008 16:37:00\n 
  42.       y     December 2008 
  43.             MMMM yyyy (YearMonthPattern) 
  44.       Y     December 2008 
  45.             MMMM yyyy (YearMonthPattern) 
  46. .EXAMPLE 
  47.     PS C:\foo> Get-Help Get-DateTime.ps1 
  48.     Left as an exercise for the reader! 
  49. .LINK 
  50.     Script posted to: 
  51.     http://www.pshscripts.blogspot.com 
  52.     MSDN Sample Page showing C# original 
  53.     http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx 
  54. #> 
  55.  
  56. ## 
  57. # Start of Script 
  58. ## 
  59.  
  60. # Create and initialise a DateTimeFormatInfo associated with the en-US culture. 
  61. $culture = [system.Globalization.CultureInfo]::CreateSpecificCulture("en-gb"
  62. $dtfi   = $culture.DateTimeFormat 
  63.  
  64. # Create a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3). 
  65. # The Gregorian calendar is the default calendar for the en-GB culture. 
  66. $dt = get-date 
  67. "FORMAT  en-gb EXAMPLE" 
  68. "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY"  
  69. "  d     {0}       "   -f $DT.ToString("d", $DI)  
  70. "        {0} {1}   "   -f $dtfi.ShortDatePattern, "(ShortDatePattern)"  
  71. "  D     {0}       "   -f $dt.ToString("D", $DTFI)  
  72. "        {0} {1}   "   -f $dtfi.LongDatePattern,  "(LongDatePattern)"  
  73. "  f     {0}       "   -f $dt.ToString("f", $DTFI)  
  74. "  F     {0}       "   -f $dt.ToString("F", $DTFI)  
  75. "        {0} {1}   "   -f $dtfi.FullDateTimePattern, "(FullDateTimePattern)"  
  76. "  g     {0}       "   -f $dt.ToString("g", $DTFI)  
  77. "  G     {0}       "   -f $dt.ToString("G", $DTFI)  
  78. "  m     {0}       "   -f $dt.ToString("m", $DTFI)  
  79. "        {0} {1}   "   -f $dtfi.MonthDayPattern, "(MonthDayPattern)"  
  80. "  M     {0}       "   -f $dt.ToString("M", $DTFI)  
  81. "        {0} {1}   "   -f $dtfi.MonthDayPattern, "(MonthDayPattern)" 
  82. "  o     {0}       "   -f $dt.ToString("o", $DTFI)  
  83. "  r     {0}       "   -f $dt.ToString("r", $DTFI)  
  84. "        {0} {1}   "   -f $dtfi.RFC1123Pattern, "(RFC1123Pattern)"  
  85. "  R     {0}       "   -f $dt.ToString("R", $DTFI)  
  86. "        {0} {1}   "   -f $dtfi.RFC1123Pattern, "(RFC1123Pattern)"  
  87. "  s     {0}       "   -f $dt.ToString("s", $DTFI)  
  88. "        {0} {1}   "   -f $dtfi.SortableDateTimePattern, "(SortableDateTimePattern)"  
  89. "  t     {0}       "   -f $dt.ToString("t", $DTFI)  
  90. "        {0} {1}   "   -f $dtfi.ShortTimePattern, "(ShortTimePattern)"  
  91. "  T     {0}       "   -f $dt.ToString("T", $DTFI)  
  92. "        {0} {1}   "   -f $dtfi.LongTimePattern, "(LongTimePattern)"  
  93. "  u     {0}       "   -f $dt.ToString("u", $DTFI)  
  94. "        {0} {1}   "   -f $dtfi.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)"  
  95. "  U     {0}\n     "   -f $dt.ToString("U", $DTFI)  
  96. "  y     {0}       "   -f $dt.ToString("y", $DTFI)  
  97. "        {0} {1}   "   -f $dtfi.YearMonthPattern, "(YearMonthPattern)"  
  98. "  Y     {0}       "   -f $dt.ToString("Y", $DTFI)  
  99. "        {0} {1}   "   -f $dtfi.YearMonthPattern, "(YearMonthPattern)"  
  100. # End of Script