TTD
TTD

Reputation: 75

How to convert milliseconds to date and time in 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

Upvotes: 4

Views: 10369

Answers (2)

stalskal
stalskal

Reputation: 1347

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"

Upvotes: 5

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

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)

Upvotes: 3

Related Questions