Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell中获取指定月份的最后一天_Powershell_Date - Fatal编程技术网

如何在powershell中获取指定月份的最后一天

如何在powershell中获取指定月份的最后一天,powershell,date,Powershell,Date,我需要从指定的地址获取上个月的最后一天。 我有一个字符串(文件名),它的末尾包含一个日期。我需要捕获日期(已完成)并获取上个月的最后日期。例如,字符串为“proemail vytvoreni_9.2017-10-16”,因此我需要获得2017年9月30日。这就是我现在拥有的: $Report = Read-Host "File name" $Date = [datetime]$Report.Substring($Report.get_Length()-10) $Last_month = $Dat

我需要从指定的地址获取上个月的最后一天。 我有一个字符串(文件名),它的末尾包含一个日期。我需要捕获日期(已完成)并获取上个月的最后日期。例如,字符串为“proemail vytvoreni_9.2017-10-16”,因此我需要获得2017年9月30日。这就是我现在拥有的:

$Report = Read-Host "File name"
$Date = [datetime]$Report.Substring($Report.get_Length()-10)
$Last_month = $Date.AddMonths(-1)
$Date_text = $Last_month.ToString().Substring(3,7)
$month_year = ($Date_text.Split("."))
$days_count = [datetime]::DaysInMonth($month_year[1],$month_year[0])
$days_count = $days_count.ToString()
$month = $month_year[0]
$year = $month_year[1]
$Date_limit = [DateTime]($month,$days_count,$year)
除了返回此错误的最后一行之外,所有操作都正常:无法将类型为“System.Object[]”的“System.Object[]”值转换为类型为“System.DateTime”。我试图通过.ToString()方法将$month和$year转换为string,但没有效果

(Get-Date).AddDays(-$(Get-Date).Day)

Saturday, September 30, 2017 2:36:19 PM


((Get-Date).AddDays(-$(Get-Date).Day)).Day

30
输出:

2017年9月30日星期六0:00:00

输出:


2017年9月30日星期六0:00:00

虽然此代码片段可以解决问题,但确实有助于提高您的文章质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。虽然此代码片段可以解决问题,但确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
$filename = "proemail vytvoreni_9.2017 2017-10-16"

# Take the date from the filename
$sub = $filename.Substring($filename.Length-10)

# make it into a date format
$filedate = Get-Date -Date $sub

# take that date 2017-10-16 and substracts its own days so in this case substract 16 days, then show me the date
(($filedate).AddDays(-$filedate.Day)).Date