Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the date modified to be formatted with AM/PM with powershell

I've currently got

Get-Item "C:\path\to\file.txt" | ForEach-Object { $_.LastWriteTime }

Which outputs like this

12/18/2018 16:54:32

But I want it to output like this

12/18/2018 4:54 PM

Is there any way I can do that?

like image 241
NoShotz Avatar asked Dec 18 '18 22:12

NoShotz


2 Answers

Use the DateTime objects formatting. More info here

Get-Item "C:\path\to\file.txt" | ForEach-Object { 
  $_.LastWriteTime.ToString("MM/dd/yyyy hh:mm:ss tt")
}
like image 122
kpogue Avatar answered Oct 22 '22 14:10

kpogue


Much simpler to just use (Get-Date -Format 'M/d/yyyy h:mm tt')

like image 35
Mark Stepan Avatar answered Oct 22 '22 14:10

Mark Stepan