Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Comparing dates with If

Reply
 
Old 01-07-2009   #1 (permalink)
NetTerry


 
 

Comparing dates with If

I'm trying to find directories more than 90 days old, but it looks like the
If comparison isn't working correctly. In this script the result should be
'True' but comes out 'False'. Suggestions?
# IfTest.ps1
# Test If on date comparison

# Get today's date and calculate the date 90 days ago
$Today = ( get-date ).ToString('MMddyyyy')
$90daysago = ( get-date ).AddDays(-90).ToString('MMddyyyy')

Write-Output $Today
Write-Output $90daysago

# Test difference in dates
If($90daysago -lt $Today)
{$Test = 'True'}
Else
{$Test = 'False'}

Write-Output "Today is later than 90 days ago: " $Test
--
Terry Edwards

My System SpecsSystem Spec
Old 01-07-2009   #2 (permalink)


Vista Home Premium 32bit
 
 

Re: Comparing dates with If

The problem is that you're comparing strings, not dates

If you write

$Today = get-date
$90daysago = (get-date).AddDays(-90)

$Today -gt $90daysago

it returns correctly true but if you compare

01072009 with 10092008, the second string is greater than the first one.
My System SpecsSystem Spec
Old 01-08-2009   #3 (permalink)
Dr J R Stockton


 
 

Re: Comparing dates with If

In microsoft.public.windows.powershell message <u4nxt6NcJHA.1268@xxxxxx
NGP04.phx.gbl>, Wed, 7 Jan 2009 11:41:10, "Marco Shaw [MVP]" <marco.shaw
@_NO_SPAM_gmail.com> posted:
Quote:
Quote:

>> # Test If on date comparison
>> # Get today's date and calculate the date 90 days ago
>> $Today = ( get-date ).ToString('MMddyyyy')
>> $90daysago = ( get-date ).AddDays(-90).ToString('MMddyyyy')
Quote:

>The problem is you're changing your datetime object to a string, then
>comparing strings.

There is nothing intrinsically wrong with doing that, given that the
year can be assumed to be four-digit. But in this case, the designated
string format is FFF.

Use an ISO 8601 format, and the string comparison will work as desired.

It is foolish to use MMddyyyy within data processing, although
MM/DD/YYYY may be wanted to provide a human interface in a few
countries.

There should, however, be no need to do the expensive conversion to
string; conversion of the representation of true (non-string) numbers
would be much better.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Searching between dates Vista General
Dates of posts Vista General
comparing dates to $null PowerShell
file dates Vista performance & maintenance
Dates appear wrong Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46