Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert milliseconds to date and time in powershell?

Tags:

powershell

I want convert milliseconds to date time follow the format mm/dd/yyyy in powershell, my code is below:

$data+= $dataList.Rows[$i]["ROOM_LASTVISITDATE"].ToString() + "`t"

The result of 1278504562790. So, how i can convert it to date time in powershell, please help me. Thanks

like image 663
TTD Avatar asked Oct 20 '25 14:10

TTD


2 Answers

To convert a epoch/unix timestamp to a human readable date with Powershell, you can use the DateTimeOffset type.

[datetimeoffset]::FromUnixTimeMilliseconds(1278504562790).DateTime

Your code could then look like this

$lastVisited = $dataList.Rows[$i]["ROOM_LASTVISITDATE"].ToString()
$data += [datetimeoffset]::FromUnixTimeMilliseconds($lastVisited) + "`t"

like image 162
staale.skaland Avatar answered Oct 22 '25 04:10

staale.skaland


Assuming the offset is the start of the UNIX epoch (01/01/1970), you could simply add the milliseconds to that offset:

$EpochStart = Get-Date 1970-01-01T00:00:00
$myDateTime = $EpochStart.AddMilliseconds(1278504562790)
like image 20
Mathias R. Jessen Avatar answered Oct 22 '25 05:10

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!