View Single Post
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