我正在努力展示里程碑。我在下面尝试但是收到错误,显示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"
答案 0 :(得分:1)
错误就在你面前:方法调用失败,因为 [System.String]不包含名为' AddDays'
的方法您正在尝试使用AddDays()
类中的DateTime
方法,但$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:")
- 方法的结果。
public static string IndentedPrint(XmlDocument doc)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode))
{
xmlTextWriter.Formatting = Formatting.Indented;
doc.WriteContentTo(xmlTextWriter);
xmlTextWriter.Flush();
memoryStream.Flush();
memoryStream.Position = 0;
using (StreamReader sr = new StreamReader(memoryStream))
{
return sr.ReadToEnd();
}
}
}
}
答案 1 :(得分: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"