Sunday, December 23, 2007

How many days in a month? and more...

 

Ask the [datetime] type :-). Give it a Year and month number.

 

PS>[datetime] | gm -static days*

   TypeName: System.DateTime

Name        MemberType Definition
----        ---------- ----------
DaysInMonth Method     static System.Int32 DaysInMonth(Int32 year, Int32 month)

 

# number of days in January 2007
PS > [datetime]::DaysInMonth(2007,1)
31

 

 

# number of days in each month of year 2007
PS > $year = 2007
PS>1..12 | foreach { "There are {0} days in: {1} {2}" -f [datetime]::DaysInMonth($year,$_),(get-date -month $_ -f MMMM),$year }

There are 31 days in: January 2007
There are 28 days in: February 2007
There are 31 days in: March 2007
There are 30 days in: April 2007
There are 31 days in: May 2007
There are 30 days in: June 2007
There are 31 days in: July 2007
There are 31 days in: August 2007
There are 30 days in: September 2007
There are 31 days in: October 2007
There are 30 days in: November 2007
There are 31 days in: December 2007

 

# another method to get month names (depending on the culture)
PS > [System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames

January
February
March
April

(...)

 

# get month names by number
PS > 1..12 | foreach { [System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames[$_] }

(...)
August
September
October
November
December

 

# with PowerShell new-object cmdlet

PS > $dtfi = new-object system.globalization.datetimeformatinfo

PS > $dtfi.DayNames

Sunday
Monday
(...)

 

PS > $dtfi.DayNames.Get(0)  # same as $a.DayNames[0]
Sunday

 

PS > $dtfi.MonthNames
January
February
March
(...)

 

PS > $dtfi.MonthNames[11]
December

 

# get month names for a specific culture
PS > ([System.Globalization.CultureInfo]"en-US").DateTimeFormat.MonthNames
January
February
March
April
(...)

 

# Hebrew day names

PS:48 >([System.Globalization.CultureInfo]"he-IL").DateTimeFormat.DayNames
יום ראשון
יום שני
יום שלישי
יום רביעי
יום חמישי
יום שישי
שבת

 

# CultureTypes Enumeration
# http://msdn2.microsoft.com/en-us/library/system.globalization.culturetypes.aspx

PS > $AllCultures = [System.Globalization.CultureTypes]::AllCultures
PS > [System.Globalization.CultureInfo]::GetCultures($AllCultures)

LCID             Name             DisplayName
----             ----             -----------
1                ar               Arabic
2                bg               Bulgarian
3                ca               Catalan
4                zh-CHS           Chinese (Simplified)
5                cs               Czech
(...) 
 
 

# format each culture date, base on its local datetime ShortDatePattern, extend each returned item with a new property (ShortDate)

$AllCultures = [System.Globalization.CultureTypes]::AllCultures
$cultures = [System.Globalization.CultureInfo]::GetCultures($AllCultures)
$cultures | foreach {
    $df = ([System.Globalization.CultureInfo]$_.name).DateTimeFormat.ShortDatePattern
    $_ | add-member -membertype noteproperty -name ShortDate -value (get-date -f $df)
    $_
} | select DisplayName,ShortDate | format-table -auto

 

# truncated list

DisplayName                                        ShortDate
-----------                                        ---------
Arabic (Saudi Arabia)                              23/12/07
Catalan (Catalan)                                  23/12/2007
Chinese (Taiwan)                                   2007/12/23
Czech (Czech Republic)                             23.12.2007
Danish (Denmark)                                   23-12-2007
German (Germany)                                   23.12.2007
Greek (Greece)                                     23/12/2007
English (United States)                            12/23/2007
Finnish (Finland)                                  23.12.2007
French (France)                                    23/12/2007
Hebrew (Israel)                                    23/12/2007


 

This is just the tip of the iceberg and there is ton more to explore.

1 comment:

Андрей said...

Thank you very much, very useful!