0

我正在尝试显示里程碑。我在下面尝试过,但我收到一个错误,显示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"
4

2 回答 2

2

错误就在您面前:方法调用失败,因为[System.String] 不包含名为“AddDays”的方法

您正在尝试使用AddDays()which 是DateTime-class 中的方法,但$date不是DateTime-object 因为您将其变成了字符串

$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")

如果要使用它来创建其他变量,则需要保留$dateDateTime-object。前任:

$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:")
于 2016-03-13T09:10:45.230 回答
0

在你的代码中做这样的事情:

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"
于 2016-03-13T04:03:33.883 回答