Reputation: 55
I want to change text 11.30 to time format 11:30 using [datetime] in powershell
$Stime = "11.30"
$time = "{00:hh:mm}" -f [datetime]$Stime
Write-Host $time
This code returns value 12:00
I tried this too -
$fromtime = "11.30"
[datetime]$fromtime12hrFormat = ([datetime]$fromtime)
$fromtime12hrFormat.ToString("hh:mm:ss")
This code returns value 12:00
Upvotes: 0
Views: 526
Reputation: 174990
"11.30"
cast to [datetime]
with en-US
locale will be interpreted as "November 30" at midnight.
Use DateTime.ParseExact()
instead:
'{0:HH:mm}' -f [datetime]::ParseExact($Stime, 'HH.mm', $null)
Upvotes: 1