Sunday, 14 June 2009

Get-JapaneseDate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays dates using the Japanese calander. 
  4. .DESCRIPTION 
  5.     This script creates a new date and time object, then displays  
  6.     things using the Japanese calendar. I have changed the original 
  7.     script slightly to show the different eras in use.  
  8. .NOTES 
  9.     File Name  : Get-JapaneseDate.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2009/06/get-japanesedateps1.html
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.globalization.japanesecalendar.getyear.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-Japanesedate.ps1' 
  19.     April 3, 1875 of the Gregorian calendar equals the following in the Japanese calendar: 
  20.        Era:        1 
  21.        Year:       8 
  22.        Month:      4 
  23.        DayOfYear:  93 
  24.        DayOfMonth: 3 
  25.        DayOfWeek:  Saturday 
  26.   
  27.     After adding 138 years and 10 months: 
  28.        Era:        4 
  29.        Year:       26 
  30.        Month:      2 
  31.        DayOfYear:  34 
  32.        DayOfMonth: 3 
  33.        DayOfWeek:  Monday 
  34. #> 
  35. ## 
  36. # start of script 
  37. ## 
  38.  
  39. # Helper function 
  40. function DisplayJapaneseDateValue{ 
  41. Param ( $DT ) # 
  42. # create a Japenese calendar 
  43. $cal=new-object System.Globalization.JapaneseCalendar 
  44.   
  45.  # display dates using that calendar 
  46. "   Era:        {0}" -f $Cal.GetEra($DT)  
  47. "   Year:       {0}" -f $Cal.GetYear($DT)  
  48. "   Month:      {0}" -f $Cal.GetMonth($DT
  49. "   DayOfYear:  {0}" -f $Cal.GetDayOfYear($DT
  50. "   DayOfMonth: {0}" -f $Cal.GetDayOfMonth($DT
  51. "   DayOfWeek:  {0}" -f $Cal.GetDayOfWeek($DT)  
  52. "" 
  53.   
  54. # Set a DateTime to April 3, 1875 of the Gregorian calendar.  
  55. # This date is in the Meiji era (era 1) 
  56. $myDT = new-object System.DateTime 1875, 4, 3,(New-Object System.Globalization.GregorianCalendar) 
  57.   
  58. # Display the values of the DateTime 
  59. "April 3, 1875 of the Gregorian calendar equals the following in the Japanese calendar:"  
  60. DisplayJapaneseDateValue $myDT  
  61.   
  62. # Add 138 years and 10 months 
  63. # This takes the date into the Heisei era (era 4) 
  64. $myDT = $myCal.AddYears( $myDT, 138 ) 
  65. $myDT = $myCal.AddMonths($myDT, 10 ) 
  66.   
  67. # Displays the values of the DateTime. 
  68.  "After adding 138 years and 10 months:"  
  69. DisplayJapaneseDateValue $myDT  
  70. # end of script 

No comments: