2017-10-27 12 views
0

指定された月の最後の日を取得する必要があります。 私は最後に日付を含む文字列(ファイル名)を持っています。私は日付を取得して(すでに完了している)、前の月の最後の日付を取得する必要があります。 、powershellで指定された月の最後の日を取得する方法

$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) 

すべてが最後の行を除いて、うまく動作します。たとえば、文字列は「proemail vytvoreni_9.2017 2017年10月16日」であるので、私はこれは私が今持っているものである9月30日2017年取得する必要がありますこのエラーを返します。 "System.Object []"型の "System.Object []"値を "System.DateTime"に変換することはできません。私は.ToString()メソッドで文字列に$月と$年を変換しようとしたが、それは助けにはならなかった

答えて

0
$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 

出力:

土曜日2017年9月30日0時00分00秒

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

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


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

30 
+1

このコードスニペットでは、[説明を含む](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)という質問が本当にあなたの投稿の質を向上させるのに役立つかもしれませんが。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。 – Clijsters

関連する問題