Converting Unix time to date time in powershell

YaroC 316 Reputation points
2022-03-10T10:20:38.423+00:00

How would I convert a log of high precision unix format timestamps going as far as nanoseconds like in example below to date time format so to understand the timeline for these logs? Ideally if it's converted down to microsecond so can be easier correlated with events coming from Event Monitor.

16469606540.730687318: some text...
16469606541.730687318: some text...
16469606543.730687318: some text...

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-03-10T14:48:58.903+00:00

    Hi @Hi @YaroC ,

    something like this?

    $unixTimeStamp = "1646866800.730687318"  
    $date1=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($unixTimeStamp))  
    $date1.ToString("yyyy-MM-dd HH:mm:ss.ffffff")  
      
    $date2 = (Get-Date 01.01.1970).AddSeconds($unixTimeStamp)  
    ($date2).ToString("yyyy-MM-dd HH:mm:ss.ffffff")  
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-03-10T12:17:06.57+00:00

    Hi @YaroC ,

    maybe this helps?

    $unixTimeStamp = "1646866800.730687318"  
    $date1=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($unixTimeStamp))  
    $date1  
      
    $date2 = (Get-Date 01.01.1970).AddSeconds($unixTimeStamp)  
    $date2  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. YaroC 316 Reputation points
    2022-03-10T12:37:44.91+00:00

    Thanks AndreasBaumgarten. So this takes only the first part of the timestamp and gives accuracy up to seconds which isn't sufficient for my case. Would you know how to convert this further down to at least milliseconds?


  3. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2022-03-10T12:46:39.767+00:00

    Hey YaroC-2432,

    how about using this one, the function seems to do the exact same job, I have just tested it:

    From:
    Convert Unix Millisecond Time In Powershell To Troubleshoot With Process Monitor
    https://pavolkutaj.medium.com/convert-unix-millisecond-time-in-powershell-to-troubleshoot-with-process-monitor-435400d83436

    $UnixTimeStamp = "1646866800.730687318"
    
    function getMillis {
        param (
            $unixTimeStamp
        )
        $epochStart = Get-Date 01.01.1970 
        $millisStamp = ($epochStart + ([System.TimeSpan]::frommilliseconds($unixTimeStamp))).ToLocalTime()
        $millisStampOutput = $millisStamp.ToString("yyyy-MM-dd HH:mm:ss.ffffff")
        $millisStampClipboard = $millisStamp.ToString("HH:mm:ss.ffffff") 
        Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        Write-Host "Datetime: $millisStampOutput" -ForegroundColor Cyan
        Write-Host "Clipping: $millisStampClipboard" -ForegroundColor Cyan
        Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"    
        $millisStampClipboard = $millisStamp.ToString("HH:mm:ss.ffffff") | clip
    }
    
    getMillis $UnixTimeStamp
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Stoyan Chalakov


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.