The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

vsplus (2) [Avatar] Offline
#1
Screenshot (sorry for cyrillic letters in error message):
PS > [datetime]::Now -lt "27.11.2007"
The '-lt' operator failed: Could not compare "26.11.2007 12:24:02" to "27.11.2007". Error: "Cannot convert value "27.11
.2007" to type "System.DateTime". Error: "Строка не распознана как действительное значение DateTime."".
At line:1 char:20
+ [datetime]::Now -lt <<<< "27.11.2007"

Up to me the
'Could not compare "26.11.2007 12:24:02" to "27.11.2007" '
is very addled message. You can't even imagine what is wrong in command looking at it.
vsplus (2) [Avatar] Offline
#2
Re: Inconsistent datetime from string conversion in PowerShell
Workaround:
If you try
[datetime]::Now -lt [datetime]:smiliearse("25.11.2007", [System.Threading.Thread]::CurrentThread.CurrentCulture)
it works OK.

It seems like a bug in [string] to [datetime] converter - Microsoft simply do not substitute actual CultureInfo in it's converter and use us-US CultureInfo always.
franck (17) [Avatar] Offline
#3
Re: Inconsistent datetime from string conversion in PowerShell
Hi vsplus

It's very complicated

The following works:

(get-date).ToShortDateString() -lt "27.11.2007"

Why do you use Dot.Net [datetime]:: , get-date makes the same. both give a datetime object back.

I hope that my answer was usefull for you

Regards
Franck

P.S. the following works also
([datetime]::Now).ToShortDateString() -lt "27.11.2007"