DateTime: strange behaviour

Hello,

this is a simple affair. However the outcome surprises me and perhaps s.b. can explain.
I define a string $myDate.
In the beginning the data has three dashes.
I consider this as incorrect. However, it goes through as a correct date. Why?

Also I don’t seem to be able to output the date at will in english or in German.
$date.ToString() outside write-host comes out in German.
The variable $date inside write-host turns out in english.

I would want to know how exactly can I set it to one or the other language using the same programming?
Here the code:

#Set-Culture en-US
$Culture = New-Object System.Globalization.CultureInfo("en-US")
# $culture=[cultureInfo]'en-US'
$myDate = "2025-05-02-13 11:01"
$result= $myDate-as [DateTime] -ne $null
write-host
write-host "`$result evaluates as $result"
$date = get-date "$myDate" -Format ($culture.DateTimeFormat.LongDateTimePattern)
write-host "The date in German = " $date.ToString()
write-host "Date in english = $date"
$date

Hi,
I can just convert $date to a string and specify the culture:
$date.tostring($Culture) # will come out in english

But why is the date with three dashes counted on to the third of may instead of throwing an error?

The DateTime parser in dotNet accepts datetime strings with ’ ', ‘.’ or ‘-’ as acceptable date separators. There may be others…

Hi,
thank you for responding.
I am aware that you can use comma as well as dash for date separation.
However, normally a space separates the actual date from the time.
So I would expect two dashes as acceptable in a date - separating month from day and year from month.
A third dash is senseless, isn’t it?
But somehow powershell accepts it and takes it for hours and counts the days up.
This shouldn’t be and forces me to use

[datetime::ParseExact($date,"dd.MM.yyyy HH:mm",$null)

Had the treatment of the $date not been muddled up here, I had liked to use:

if ([string]$date -as [DateTime])

Regards

Andreas

I am concerned about first checking for a correct date and I do not want s.b. who uses my script to be able to enter a date with three dashes. But as Powershell would incorrectly allow him to do just that I have to check otherwise.

Also you can see in ParseExact that the design actually is two dashes in the date (and not three) and then a space that separates time from date.

Well I used a dot, but you could replace it with a dash.

If you depend on user input you should always validate the input. A great way to avoid invalid input is to limit the choice you give the user. In a case like this case you could use a date picker and if really needed a time picker.

Hi,
I am an administrator. But the only one who (can write) writes those scripts.
My colleagues are not normal users but also administrators. They open the script and change the date.
For them, I don’t need a picker.
However, being inattentive a third dash could slip in, which I don’t want.

three dashes in the date part, to me seems to be a bug in powershell.

IMHO that’s not a good idea anyway … even if they are admins as well. How about using parameters for the needed input? I’d expect that to be way less error prone.

You may file a bug report. :point_up: :man_shrugging:

Of course they only change the parameter and not code.
After they change the parameter I check whether the parameter has a correct date.
And I do not want a parameter with three dashes where the day is then counted up, even though Powershell would accept it.