![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||