我需要检索当前月份的天数,尽管我进行了研究,但我还没有在powershell中找到任何可以做到这一点的东西。下面是我目前为了得到我想要的结果而构建的代码。有没有更好的方法呢?
请注意,我仅限于Powershell
#check to see if this is a leap year
function LYC
{
if ([System.DateTime]::isleapyear((Get-Date).Year))
{
$Global:LY = True
}
}
#Get the number of days in current month
function fNOD
{
$MNum = (Get-Date).Month
switch ($MNum)
{
1 {$Global:DaysInMonth=31}
2 {
LYC
if (LY)
{
$Global:DaysInMonth=29
} else {
$Global:DaysInMonth=28
}
}
3 {$Global:DaysInMonth=31}
4 {$Global:DaysInMonth=30}
5 {$Global:DaysInMonth=31}
6 {$Global:DaysInMonth=30}
7 {$Global:DaysInMonth=31}
8 {$Global:DaysInMonth=31}
9 {$Global:DaysInMonth=30}
10 {$Global:DaysInMonth=31}
11 {$Global:DaysInMonth=30}
12 {$Global:DaysInMonth=31}
}
}发布于 2013-03-18 11:37:05
http://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth.aspx
# static int DaysInMonth(int year, int month)
[DateTime]::DaysInMonth(2013, 3)发布于 2013-05-14 14:57:58
假设您想将它用于存储在$date中的任何日期
((get-date $date -Day 1 -hour 0 -Minute 0 -Second 0).AddMonths(1).AddSeconds(-1)).Day发布于 2013-03-18 11:39:32
在我看来,这似乎很容易:
$numDays = ((Get-Date -Month 3) - (Get-Date -Month 2))2月的28天,7月减去6月给我30天。
正如下面提到的,这显然有时会产生一些奇怪的整数舍入,给出29d,23h,59m或其他不正确的东西。
这似乎是一致的:
$monthLess = Get-Date -Month 4
$NumDays = (Get-Date -Month 5).Subtract($monthLess)https://stackoverflow.com/questions/15469185
复制相似问题