我正在从数据库中读取一些数据,所涉及的值是日期和时间戳。
为了简短起见,数据类似于此。 2015年7月24日16:13:58
然后我就像这样提取日期。
$ScanDate = $($Row[3]).ToString()
[String]$Scan = $ScanDate.SubString(0,10)
然后我将$ ScanDate拆分为数组
$ Scan.Split( '/')
然后我将每天,每月和每年的值更改为整数
[int]$ScanMonth = $Scan[0]
[int]$ScanDay = $Scan[1]
[int]$ScanYear = $Scan[2]
检查它们是否为整数..
$ScanDay.GetType()
$ScanYear.GetType()
$ScanMonth.GetType()
退货..
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
True True Int32 System.ValueType
True True Int32 System.ValueType
问题是当我尝试添加和减去那些整数时,我得到了错误的结果?
$Total = [int]$ScanDay + [int]$ScanMonth
Write-Host $Total
所以24 + 7的值为102而不是31。
我缺少什么?任何帮助表示赞赏:)
更新
好的,所以我想添加最终的比较代码。在被告知数据库值被powershell解释为Datetime后,我可以做这样的事情。
$Date = Get-Date
$Date = $Date.ToUniversalTime()
$DayDifference = New-TimeSpan -Start $Date -End $Row[3]
if ($DayDifference.TotalDays -gt 5) { 'Do Something' }
答案 0 :(得分:0)
$scan = "7/24/2015 16:13:58"
$scan = $scan.Split('/');
[int]$ScanMonth = $Scan[0]
[int]$ScanDay = $Scan[1]
[int]$ScanYear = ($Scan[2].Split())[0]
$Total = [int]$ScanDay + [int]$ScanMonth
Write-Host $Total
然后你会得到31 ..虽然我仍然认为你应该使用DateTime对象
更新 :解决实际问题的更简单方法:
$scan = "7/24/2015 16:13:58"
$scanDate = [DateTime]$scan
$date = (get-date).addDays(-5)
if($date -lt $scanDate) {'Do Something'}