【问题标题】:How to display the Current Month in Plain Text Format?如何以纯文本格式显示当前月份?
【发布时间】:2013-01-03 11:49:25
【问题描述】:

关于如何以纯文本格式将日历的当前月份快速打印到剪贴板或 Windows 环境中的命令,有什么建议吗?为了清楚起见,我希望看到完整的打印月份,类似于单击 Windows 任务栏中的时钟时看到的内容。我正在考虑一个轻量级的 PowerShell 脚本,或者可能有一些其他预打包的 Windows 应用程序功能可以让我轻松地做到这一点。

【问题讨论】:

    标签: windows powershell keyboard-shortcuts plaintext


    【解决方案1】:

    试试这个:

    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
    

    【讨论】:

    • 这里要清楚一点,我希望看到完整的打印月份,类似于单击 Windows 任务栏中的时钟时看到的内容。
    【解决方案2】:
    new-alias  Out-Clipboard $env:SystemRoot\system32\clip.exe
    (get-date -Format MMMM) |Out-Clipboard
    

    【讨论】:

      【解决方案3】:

      日历(将线性日期分解为日/月/年的系统)没有当前任何内容。

      但是,如果您的意思是在给定日历中获取 DateTime 的月份,您需要查看指定 CultureInfo ,它在 .NET 中包含选定的日历:

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

      有多种方法可以创建自定义CultureInfo 或(更一般地)DateTimeFormatInfo 的实例并作为DateTime.ToString() 的第二个参数传递。

      最简单的方法是获取一个使用目标日历的CultureInfo实例,例如:

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

      MSDN 有一整页介绍使用日历:http://msdn.microsoft.com/en-us/library/82aak18x%28v=vs.100%29.aspx(这是 PowerShell 3 的 .NET 4 版本)。

      【讨论】:

      • 这里要清楚一点,我希望看到完整的打印月份,类似于单击 Windows 任务栏中的时钟时看到的内容。
      猜你喜欢
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      相关资源
      最近更新 更多