Pregunta

Any suggestions on how I can quickly print the current month of a calendar in plain text to the clipboard or to the command in the a windows environment? To be clear here, I would like to see the full printed month, similar to what you see when you single click on the clock in the windows taskbar. I'm thinking along the lines of a a lightweight PowerShell script or perhaps there is some other pre-packaged Windows application functionality that would allow me to do this easily.

¿Fue útil?

Solución

Give this a try:

function Get-Calendar($year=(Get-Date).Year,$month=(Get-Date).Month){
    $dtfi = New-Object System.Globalization.DateTimeFormatInfo
    $AbbreviatedDayNames=$dtfi.AbbreviatedDayNames | ForEach-Object {" {0}" -f $_.Substring(0,2)}

    $header= "$($dtfi.MonthNames[$month-1]) $year"
    $header=(" "*([math]::abs(21-$header.length) / 2))+$header
    $header+=(" "*(21-$header.length))

    Write-Host $header -BackgroundColor yellow -ForegroundColor black
    Write-Host (-join $AbbreviatedDayNames) -BackgroundColor cyan -ForegroundColor black
    $daysInMonth=[DateTime]::DaysInMonth($year,$month)

    $dayOfWeek =(New-Object DateTime $year,$month,1).dayOfWeek.value__
    $today=(Get-Date).Day

    for ($i = 0; $i -lt $dayOfWeek; $i++){Write-Host (" "*3) -NoNewline}
    for ($i = 1; $i -le $daysInMonth; $i++)
    {
        if($today -eq $i){Write-Host ("{0,3}" -f $i) -NoNewline -BackgroundColor red -ForegroundColor white}
        else {Write-Host ("{0,3}" -f $i) -NoNewline -BackgroundColor white -ForegroundColor black}

        if ($dayOfWeek -eq 6) {Write-Host}
        $dayOfWeek = ($dayOfWeek + 1) % 7
    }
    if ($dayOfWeek -ne 0) {Write-Host}
}

PS> Get-Calendar

    January 2013
 Su Mo Tu We Th Fr Sa
        1  2  3  4  5
  6  7  8  9 10 11 12
 13 14 15 16 17 18 19
 20 21 22 23 24 25 26
 27 28 29 30 31

Otros consejos

new-alias  Out-Clipboard $env:SystemRoot\system32\clip.exe
(get-date -Format MMMM) |Out-Clipboard

A calendar (a system for breaking a linear date into day/month/year) doesn't have a current anything.

But if you mean get the month of a DateTime in a given calendar you need to look at specifying a CultureInfo which in .NET incorporates a selected calendar:

$d = Get-Date
$d.ToString('MMMM', [System.Globalization.CultureInfo]::CurrentCulture)

There are a number of ways to create a custom CultureInfo or (more generally) an instance of DateTimeFormatInfo and passing as the second parameter of DateTime.ToString().

The simplest approach is to get an instance of CultureInfo that uses the target calender, eg.:

$ci = [System.Globalization.CultureInfo]::CreatedSpecifiedCulture('jp-jp')

MSDN has a whole page on working with calendars: http://msdn.microsoft.com/en-us/library/82aak18x%28v=vs.100%29.aspx (this is the .NET 4 version for PowerShell 3).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top