当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 星期五乐趣 - 有人真的知道今天是什么日子吗?

作者:精品下载站 日期:2024-12-14 07:43:56 浏览:14 分类:玩电脑

星期五乐趣 - 有人真的知道今天是什么日子吗?


[玩转系统] 星期五乐趣 - 有人真的知道今天是什么日子吗?

#requires -version 2.0

Function Get-Calendar {

<#
.Synopsis
Display a monthly calendar
.Description
This command will display calendar month based on the given date.
.Example
PS C:\> get-calendar

      September 2014
Sun Mon Tue Wed Thu Fri Sat
      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
 .Example
 PS C:> 1..12 | foreach {get-calendar "$_/1/2015"} | Out-file c:\work15.txt
Create a calendar text file for 2015 using a North American date format.
.Notes
Last Updated: September 16, 2014
Version     : 1.0

Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones6/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell 3 in a Month of Lunches (http://manning.com/jones3/)
 Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)

.Link
https://jdhitsolutions.com/blog/2014/09/friday-fun-does-anyone-really-know-what-day-it-is
.Link
Get-Date
Get-Culture
#>

[cmdletbinding()]
Param(
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
[datetime]$Date = (Get-Date)
)

#get some information about weekday names that is culture specific
$cult = Get-Culture
$cal = $cult.Calendar
$ui = Get-UICulture

$daysOfWeek = $ui.DateTimeFormat.AbbreviatedDayNames
$startWeek = $daysOfWeek[0]
#create a string with the days of the week
$days = $DaysOfWeek -join " "

#how many days in the month
$intDays = $cal.GetDaysInMonth($Date.year,$Date.month)

#get the first day of the month
[datetime]$firstday = $Date.addDays(-$Date.Day+1)

<#
I want to align each row of weekdays with the days of the week header.
I need to align the first day of the first week.
#>

$week1=@()
for ($i=0;$i -lt 7;$i++) {
if (("{0:ddd}" -f $firstday) -eq $ui.DateTimeFormat.AbbreviatedDayNames[$i] ) {
  #I am assuming all short day abbreviations will be 3 characters
  $week1+=$firstday.Day.ToString().PadLeft(3)
  #got the date so move on
  break
}
else {
 #add an empty day
 $week1+="   "
} 
}

#fill out the rest of the week
$j = 1
#loop through until we reach the end of the week
while ($i -le 5) {
    $week1+=$firstday.AddDays($j).day.ToString().PadLeft(3)
    $i++
    $j++
}

#construct a centered header
$MonthYear = "{0:MMMM yyyy}" -f $Date
$pad = ($days.Length/2) + ($Monthyear.length/2)
$title = $monthYear.PadLeft($pad)

#define a here string for the final calendar output
$myCal=@"

$title
$days
$($week1 -join " ")`n
"@

#process the rest of the weeks
for ($k=2;$k -le 5;$k++ ) {
 #initialize an array for a week of days
 $wkArray = @()
 for ($i = 0; $i -le 6; $i++) { 
  #get the next day
  $nextday = $firstday.AddDays($j)

  #test if we are still in the same month
  if ($nextday.Month -eq $firstday.month) {
    #add the padded day to the array
    $wkArray+= $nextday.day.ToString().PadLeft(3)
    $j++
  } 
  else {
    #new month so break out of the inner For loop
    Break
  }
 }
 #join each element of the array into a single line
 #and add to the calendar string
 $myCal+= "$($wkArray -join " ")`n"
}

#write out as separate lines which makes it easier to save to a file
$mycal.Split("`n")

} #end function

Set-Alias -Name gcal -Value Get-Calendar

该脚本将为该函数定义一个别名 gcal。

因为日历是特定于文化(即语言)的,所以我想编写任何人都可以使用的东西,而不仅仅是北美用户的东西。这意味着检索文化设置,以便我可以使用一周中的几天的特定语言版本。我认为。如果系统没有完全在不同的文化中运行,则很难对其进行全面测试。

我希望我的日历能够以正确的文化显示缩写的工作日名称。

$cult = Get-Culture
$cal = $cult.Calendar
$ui = Get-UICulture

$daysOfWeek = $ui.DateTimeFormat.AbbreviatedDayNames

经过相当多的实验后,我意识到创建日历的最佳方法是将每一行编写为单独的字符串。这意味着一行将是缩写的工作日名称。

#create a string with the days of the week
$days = $DaysOfWeek -join " "

那么每周都会是一串数字。但这是棘手的部分。很容易找出该月有多少天,以及该月的第一天是哪一天。

$intDays = $cal.GetDaysInMonth($Date.year,$Date.month)

#get the first day of the month
[datetime]$firstday = $Date.addDays(-$Date.Day+1)

挑战如下:我可以算出该月的第一天可能是星期四。这意味着我需要在 THU 标题下对齐“1”,然后添加足够的其他天数,直到到达周末。

$week1=@()
for ($i=0;$i -lt 7;$i++) {
if (("{0:ddd}" -f $firstday) -eq $ui.DateTimeFormat.AbbreviatedDayNames[$i] ) {
  #I am assuming all short day abbreviations will be 3 characters
  $week1+=$firstday.Day.ToString().PadLeft(3)
  #got the date so move on
  break
}
else {
 #add an empty day
 $week1+="   "
} 
}

因此,我创建了一个数组 $Week1 来保存一组值。我使用 FOR 循环来检查日期的短日期名称是否与 UI 区域性中的缩写日期名称数组匹配,我知道这是该月的第一天。否则,我只需向数组添加一个 2 个空格的“填充符”。然后我需要添加第一周的其余天数。

#fill out the rest of the week
$j = 1
#loop through until we reach the end of the week
while ($i -le 5) {
    $week1+=$firstday.AddDays($j).day.ToString().PadLeft(3)
    $i++
    $j++
}

这样,我就可以开始使用此处的字符串构建日历的文本表示形式。

#define a here string for the final calendar output
$myCal=@"

$title
$days
$($week1 -join " ")`n
"@

巧妙的部分是使用 -Join 运算符本质上连接第 1 周数组中的所有值,并用空格分隔每个值。从这里开始,我每周重复这个过程,直到月底。

在该过程结束时,$mycal 是带有日历的字符串。我可以简单地将 $mycal 写入管道,它看起来就很好。但是,因为您可能希望将日历保存到文本文件中,所以我发现最好将此处的字符串拆分回数组中。

#write out as separate lines which makes it easier to save to a file
$mycal.Split("`n")

现在您可以创建明年的文本文件日历:

1..12 | foreach {get-calendar "$_/1/2015"} | Out-file c:\work15.txt

或者在提示符下运行命令。

[玩转系统] 星期五乐趣 - 有人真的知道今天是什么日子吗?

该函数默认为当前月份,或者您可以指定任何日期。你甚至可以把它写给主机并添加一点颜色。

[玩转系统] 星期五乐趣 - 有人真的知道今天是什么日子吗?

如果您在 en-US 以外的其他环境下运行 PowerShell,我很想听听这对您有何帮助。希望您能从中获得一点乐趣。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯