Subtract/Add 天到使用 Write-Host 的预定义日期
Subtract/Add days to a predefined date using Write-Host
我正在尝试显示里程碑。我在下面尝试过,但出现错误,显示 Method invocation failed because [System.String] does not contain a method named 'AddDays'.
我在前一行中预定义了 $lastmodified
,它是 12/28/2015 0:00
$predetermined=[system.datetime]$LastModified
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")
$date5 = ($date).AddDays(-15).ToString("MM/dd/yyyy:")
$date4 = ($date).AddDays(-12).ToString("MM/dd/yyyy:")
$date3 = ($date).AddDays(-9).ToString("MM/dd/yyyy:")
$date2 = ($date).AddDays(-6).ToString("MM/dd/yyyy:")
$date1 = ($date).AddDays(-3).ToString("MM/dd/yyyy:")
write-host -foregroundcolor Green "$date5 Action 1"
write-host -foregroundcolor Green "$date4 Action 2"
write-host -foregroundcolor Green "$date3 Action 3"
write-host -foregroundcolor Green "$date2 Action 4"
write-host -foregroundcolor Green "$date1 Action 5"
write-host -foregroundcolor Green "$date Action 6"
我的输出应该是
12/23/2015: Action 1"
12/26/2015: Action 2"
12/19/2015: Action 3"
12/22/2015: Action 4"
12/25/2015: Action 5"
12/28/2015: Action 6"
在你的代码中做这样的事情:
function Get-DateFmt1
{
param (
[Parameter(Mandatory=$true, Position=0)]
[DateTime] $Date
)
$Date.ToString("MM/dd/yyyy")
}
$LastModified = [DateTime]::Parse('12/28/2015')
# ....
$predetermined=[DateTime]$LastModified
$date = ($predetermined).AddDays(30)
$date5 = $date.AddDays(-15)
$date4 = $date.AddDays(-12)
$date3 = $date.AddDays(-9)
$date2 = $date.AddDays(-6)
$date1 = $date.AddDays(-3)
Write-Host -Foregroundcolor Green "$($date5.ToString("MM/dd/yyyy")): Action 1"
Write-Host -Foregroundcolor Green "$($date4.ToString("MM/dd/yyyy")): Action 2"
Write-Host -Foregroundcolor Green "$($date3.ToString("MM/dd/yyyy")): Action 3"
# or write a helper function if that makes sense to you
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date2): Action 4"
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date1): Action 5"
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date): Action 6"
错误就在眼前:方法调用失败,因为[System.String]不包含名为'AddDays'[=26=的方法]
您正在尝试使用 AddDays()
,这是 DateTime
-class 中的一个方法,但 $date
不是 DateTime
对象,因为你把它变成了一个字符串
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")
如果您要使用它来创建其他变量,则需要将 $date
保留为 DateTime
对象。例如:
$predetermined=[system.datetime](get-date)
$date = $predetermined.AddDays(30)
$date5 = $date.AddDays(-15).ToString("MM/dd/yyyy:")
$date4 = $date.AddDays(-12).ToString("MM/dd/yyyy:")
$date3 = $date.AddDays(-9).ToString("MM/dd/yyyy:")
$date2 = $date.AddDays(-6).ToString("MM/dd/yyyy:")
$date1 = $date.AddDays(-3).ToString("MM/dd/yyyy:")
#Convert `$date` to string using specified format
$date0 = $date.ToString("MM/dd/yyyy:")
write-host -foregroundcolor Green "$date5 Action 1"
write-host -foregroundcolor Green "$date4 Action 2"
write-host -foregroundcolor Green "$date3 Action 3"
write-host -foregroundcolor Green "$date2 Action 4"
write-host -foregroundcolor Green "$date1 Action 5"
write-host -foregroundcolor Green "$date0 Action 6"
更新:故障排除步骤.. 您收到的错误不仅仅是消息。它还说是哪一行导致了错误。
Method invocation failed because [System.String] does not contain a method named 'AddDays'.
At line:4 char:1
+ $date5 = ($date).AddDays(-15).ToString("MM/dd/yyyy:")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
上面还说AddDays()
不是System.String
中的方法,那我们看看你反对你在AddDays()
上调用了什么:
($date).AddDays(-15)...
这意味着 $date
是一个字符串对象。为什么会这样?那是因为它包含 ToString()
方法的结果。
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")
我正在尝试显示里程碑。我在下面尝试过,但出现错误,显示 Method invocation failed because [System.String] does not contain a method named 'AddDays'.
我在前一行中预定义了 $lastmodified
,它是 12/28/2015 0:00
$predetermined=[system.datetime]$LastModified
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")
$date5 = ($date).AddDays(-15).ToString("MM/dd/yyyy:")
$date4 = ($date).AddDays(-12).ToString("MM/dd/yyyy:")
$date3 = ($date).AddDays(-9).ToString("MM/dd/yyyy:")
$date2 = ($date).AddDays(-6).ToString("MM/dd/yyyy:")
$date1 = ($date).AddDays(-3).ToString("MM/dd/yyyy:")
write-host -foregroundcolor Green "$date5 Action 1"
write-host -foregroundcolor Green "$date4 Action 2"
write-host -foregroundcolor Green "$date3 Action 3"
write-host -foregroundcolor Green "$date2 Action 4"
write-host -foregroundcolor Green "$date1 Action 5"
write-host -foregroundcolor Green "$date Action 6"
我的输出应该是
12/23/2015: Action 1"
12/26/2015: Action 2"
12/19/2015: Action 3"
12/22/2015: Action 4"
12/25/2015: Action 5"
12/28/2015: Action 6"
在你的代码中做这样的事情:
function Get-DateFmt1
{
param (
[Parameter(Mandatory=$true, Position=0)]
[DateTime] $Date
)
$Date.ToString("MM/dd/yyyy")
}
$LastModified = [DateTime]::Parse('12/28/2015')
# ....
$predetermined=[DateTime]$LastModified
$date = ($predetermined).AddDays(30)
$date5 = $date.AddDays(-15)
$date4 = $date.AddDays(-12)
$date3 = $date.AddDays(-9)
$date2 = $date.AddDays(-6)
$date1 = $date.AddDays(-3)
Write-Host -Foregroundcolor Green "$($date5.ToString("MM/dd/yyyy")): Action 1"
Write-Host -Foregroundcolor Green "$($date4.ToString("MM/dd/yyyy")): Action 2"
Write-Host -Foregroundcolor Green "$($date3.ToString("MM/dd/yyyy")): Action 3"
# or write a helper function if that makes sense to you
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date2): Action 4"
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date1): Action 5"
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date): Action 6"
错误就在眼前:方法调用失败,因为[System.String]不包含名为'AddDays'[=26=的方法]
您正在尝试使用 AddDays()
,这是 DateTime
-class 中的一个方法,但 $date
不是 DateTime
对象,因为你把它变成了一个字符串
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")
如果您要使用它来创建其他变量,则需要将 $date
保留为 DateTime
对象。例如:
$predetermined=[system.datetime](get-date)
$date = $predetermined.AddDays(30)
$date5 = $date.AddDays(-15).ToString("MM/dd/yyyy:")
$date4 = $date.AddDays(-12).ToString("MM/dd/yyyy:")
$date3 = $date.AddDays(-9).ToString("MM/dd/yyyy:")
$date2 = $date.AddDays(-6).ToString("MM/dd/yyyy:")
$date1 = $date.AddDays(-3).ToString("MM/dd/yyyy:")
#Convert `$date` to string using specified format
$date0 = $date.ToString("MM/dd/yyyy:")
write-host -foregroundcolor Green "$date5 Action 1"
write-host -foregroundcolor Green "$date4 Action 2"
write-host -foregroundcolor Green "$date3 Action 3"
write-host -foregroundcolor Green "$date2 Action 4"
write-host -foregroundcolor Green "$date1 Action 5"
write-host -foregroundcolor Green "$date0 Action 6"
更新:故障排除步骤.. 您收到的错误不仅仅是消息。它还说是哪一行导致了错误。
Method invocation failed because [System.String] does not contain a method named 'AddDays'.
At line:4 char:1
+ $date5 = ($date).AddDays(-15).ToString("MM/dd/yyyy:")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
上面还说AddDays()
不是System.String
中的方法,那我们看看你反对你在AddDays()
上调用了什么:
($date).AddDays(-15)...
这意味着 $date
是一个字符串对象。为什么会这样?那是因为它包含 ToString()
方法的结果。
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")