0

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

1
  • you need to deal with the ambiguous date time format. something like this >>> [datetime]::ParseExact($STime, 'HH.mm', $Null) <<< should do the job. [grin] that gives me 11:30 am with the date set to today. Commented May 29, 2020 at 15:00

1 Answer 1

1

"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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.