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 

No comments: