PowerShell日期函数问题

nne*_*urg 1 powershell date

我需要PowerShell函数将23个月添加到当前日期,但将日期设置为第一个.

我尝试了一个while循环来减去日子,但我没有运气.我有月份部分想通了,但我需要将$ current.day设置为常量1.

function Update-Date {
    $current = Get-Date
    $current.AddMonths(11)
    $current.AddYears(1)
    $current.Day(1)
}
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 5

的Get-Date小命令可以调整通过其指定的日期(的拷贝)组件-Day,-Month和-Year(对于时间部分和类似)的参数,所以尝试以下方法:

Get-Date -Day 1 (Get-Date).Date.AddMonths(23)
Run Code Online (Sandbox Code Playgroud)

请注意使用.Date,删除日期中的时间部分.

包含在您的函数中(为了与PowerShell的命名约定重新命名,请注意该函数输出结果日期):

function Get-UpdateDate { Get-Date -Day 1 (Get-Date).Date.AddMonths(23) }
Run Code Online (Sandbox Code Playgroud)