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 

No comments: