none
Converting Epoch time to datetime RRS feed

  • Question

  • I have managed to convert from Unix time (time since 1970 in milliseconds) with the following:

    Dim epochdate As DateTime
    Dim seconds_since_epoch As Double
    Dim converted_datetime As Date
    
    epochdate = #01/01/1970 00:00:00# 
    
    seconds_since_epoch = 1531850023.185
    
    converted_datetime = DateAdd("s", seconds_since_epoch, epochdate) '
    

    However, the 'converted_datetime' is correct but doesnt include milliseconds.  Is there a way to get milliseconds?


    Monday, March 16, 2020 3:07 PM

All replies

  • I get Milliseconds if I test =>

        Public Shared Function UnixTimeStampToDateTime(ByVal nUnixTimeStamp As Double) As DateTime
            Dim dtDateTime As System.DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)
            dtDateTime = dtDateTime.AddSeconds(nUnixTimeStamp).ToLocalTime()
            Return dtDateTime
        End Function

    Dim nSeconds_since_epoch As Double = 1531850023.185
    Dim dtResult As DateTime = UnixTimeStampToDateTime(nSeconds_since_epoch)
    Dim sMilliseconds As String = dtResult.ToString("fff")

    Monday, March 16, 2020 4:04 PM
  • THanks, that does indeed work.

    Is there a way I can get that dtResult formatted as YYYY/MM/DD HH:MM:SS.SSS?

    Monday, March 16, 2020 4:12 PM
  • Try using .ToString

    Dim seconds_since_epoch As Double = 1531850023.185
    Console.WriteLine(UnixTimeStampToDateTime(seconds_since_epoch).ToString("yyyy/MM/dd hh:mm:ss"))


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 16, 2020 4:16 PM
    Moderator
  • THanks, that does indeed work.

    Is there a way I can get that dtResult formatted as YYYY/MM/DD HH:MM:SS.SSS?

    You can do :

    Dim sDateResult As String = dtResult.ToString("yyyy/MM/dd hh:mm:ss.fff")

    Monday, March 16, 2020 4:20 PM
  • Hi Mister S,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 26, 2020 8:03 AM
    Moderator