整数不加/减
Integers are not adding / subtracting
我正在从数据库中读取一些数据,所讨论的值是日期和时间戳。
简而言之,数据与此类似。
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()
Returns..
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 解释为日期时间后,我可以做任何事情。
$Date = Get-Date
$Date = $Date.ToUniversalTime()
$DayDifference = New-TimeSpan -Start $Date -End $Row[3]
if ($DayDifference.TotalDays -gt 5) { 'Do Something' }
$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'}
我正在从数据库中读取一些数据,所讨论的值是日期和时间戳。
简而言之,数据与此类似。 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()
Returns..
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 解释为日期时间后,我可以做任何事情。
$Date = Get-Date
$Date = $Date.ToUniversalTime()
$DayDifference = New-TimeSpan -Start $Date -End $Row[3]
if ($DayDifference.TotalDays -gt 5) { 'Do Something' }
$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'}