I am reading some data from a Database and the value in question is a Date and Time Stamp.
To keep things short the Data is similar to this. 7/24/2015 16:13:58
I then extract the Date only like this.
$ScanDate = $($Row[3]).ToString()
[String]$Scan = $ScanDate.SubString(0,10)
I then split the $ScanDate into an Array
$Scan.Split('/')
Then i change the Value to an Integer for each Day, Month and Year
[int]$ScanMonth = $Scan[0]
[int]$ScanDay = $Scan[1]
[int]$ScanYear = $Scan[2]
Check they are Integers..
$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
The issue is when i try and add and subtract those Integers, I am getting the wrong result?
$Total = [int]$ScanDay + [int]$ScanMonth
Write-Host $Total
So the values 24 + 7 come out as 102 not 31.
What am i missing? Any help appreciated :)
Updated
Ok so i wanted to add the final compare code..After being told the Database value was interpreted by powershell as a Datetime I could do everything like this.
$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
Then you'll get 31.. Although I still think you should be using DateTime object
Update : An easier way to solve your actual problem:
$scan = "7/24/2015 16:13:58"
$scanDate = [DateTime]$scan
$date = (get-date).addDays(-5)
if($date -lt $scanDate) {'Do Something'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.