time Between Dates..

by Richie88 at 2013-02-13 02:00:16

Hey Guys,

Hope all is well in the scripting world…

Taken on a bit of a project thats slightly over my head, having no real scripting experience before but powering through!

I’ve created a few different scripts which interact with each other which is great, lots of reading from CSV’s, removing, renaming, adding, filtering etc! And pleased with how they are all running.

Now the only problem I have is less than and greater than IF statements!

Seems I’ve read quite a bit online in regards to this, tried converting to datetime format and a few other things but just can’t get the correct answer.

I basically want to search a CSV file and anything over 75 days to discard.

The CSV has a date field, dd/mm/yyyy but with no time.

I will paste my script below and hopefully we can go from there!!

Cheers guys :slight_smile:

$filename = $Args[0]
$backdate = [DateTime]::Now.AddDays(-75)
$backdate = $backdate.ToShortDateString()

$header = 'Date', 'Cust ID', 'Loan No'
$out = Import-Csv -Path $filename |
Select-Object -Skip 1 | ForEach-Object {
$date = (Get-Date $.Date).ToShortDateString()
Write-Host $date
if ($backdate -gt $date) {

$out | Export-Csv -Path $filename -NoTypeInformation
by Klaas at 2013-02-13 02:26:50
Why don’t you just skip the .Toshortdatestring() methods?
You want to compare dates, not strings, right?
as a date: 20/11/2012 is lesser than 19/12/2012, but as a string it’s greater (2 > 1).
by Richie88 at 2013-02-13 03:24:25
Hey Klaas,

Sorry that was the last modified version that I tried, didn’t see that it was setting it as a string!

I’ll remove that part and try again when I’m at work…

Sometimes 8+ hours of this stuff makes you miss the little things haha