# DateTime: strange behaviour

**URL:** https://forums.powershell.org/t/datetime-strange-behaviour/25747
**Category:** PowerShell Help
**Created:** [April 28, 2025, 1:50pm UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747 "2025-04-28T13:50:03Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 28, 2025, 1:50pm UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/1 "2025-04-28T13:50:03Z")

</div>

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:

```auto
#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

```

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 28, 2025, 2:13pm UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/2 "2025-04-28T14:13:51Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)
#### Post date: [April 28, 2025, 3:13pm UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/3 "2025-04-28T15:13:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 8:23am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/4 "2025-04-29T08:23:42Z")

</div>

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

```auto
[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:

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

```

Regards

Andreas

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 8:25am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/5 "2025-04-29T08:25:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 8:27am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/6 "2025-04-29T08:27:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 8:27am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/7 "2025-04-29T08:27:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 29, 2025, 9:18am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/8 "2025-04-29T09:18:48Z")

</div>

> [@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.

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.

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 10:16am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/9 "2025-04-29T10:16:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 10:17am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/10 "2025-04-29T10:17:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 29, 2025, 10:56am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/11 "2025-04-29T10:56:34Z")

</div>

> [@Andreas](#):
>
> They open the script and change the date.

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.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 29, 2025, 10:57am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/12 "2025-04-29T10:57:12Z")

</div>

> [@Andreas](#):
>
> three dashes in the date part, to me seems to be a bug in powershell.

You may file a bug report. ☝ 🤷‍♂️

---

<div class="post-metadata">

### Author: ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forums.powershell.org/u/Andreas)
#### Post date: [April 29, 2025, 11:41am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/13 "2025-04-29T11:41:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [May 29, 2025, 11:41am UTC](https://forums.powershell.org/t/datetime-strange-behaviour/25747/14 "2025-05-29T11:41:59Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
