[玩转系统] 星期五乐趣 - 有人真的知道今天是什么日子吗?
作者:精品下载站 日期: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,我很想听听这对您有何帮助。希望您能从中获得一点乐趣。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag