none
vb.net 2015 timestamp RRS feed

  • Question

  • Hi,

    i'm trying to display a date to timestamp unix.

    my format date is this:

     Dim format As String = ("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'")

    then displaying to textbox1.text I do:

     Dim time1 As DateTime = DateTime.Now
            TextBox1.Text = time1.ToString(format)

    Tue, 23 may 2017 12:16:57 GMT

    result is what I'm looking for,

    but How can I convert this date to timestamp?

    second question if  I have an integer  '1495530867752' how to convert to my date?

    thank you for your help

    Tuesday, May 23, 2017 10:21 AM

Answers

  • Nice puzzle. 

    For that I did not look at the Giro, but I think I got it.

    Module Module1
        Sub Main()
            Dim UnixTimeStampNow = New DateTime(2017, 5, 23, 9, 14, 23, 752).Subtract(New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds
            Console.WriteLine(UnixTimeStampNow)
            Console.ReadKey()
        End Sub
    End Module

    Thanks to Les, who taught me it are milliseconds.


    Success
    Cor

    Tuesday, May 23, 2017 1:08 PM

All replies

  • Hi

    What is that 'integer'? It is to large to be an Integer and can not be represented as an Integer in VB.NET.


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, May 23, 2017 4:32 PM
    Tuesday, May 23, 2017 10:53 AM
  • forget the integer.

    what about How can I convert this date to timestamp?

    Tue, 23 may 2017 12:16:57 GMT

    thank you

    Tuesday, May 23, 2017 10:57 AM
  • Tuesday, May 23, 2017 10:58 AM
  • Hello,

    DateTime does not store or know how to work with time zones. Look at using DateTimeOffset.

    https://msdn.microsoft.com/en-us/library/bb546100(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/bb546101%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, May 23, 2017 10:59 AM
    Moderator
  • forget the integer.

    what about How can I convert this date to timestamp?

    Tue, 23 may 2017 12:16:57 GMT

    thank you

    Hi

    OK, try some of this:

            ' your date as provided
            Dim i As Double = GetUnixTimestampFromVBdatetime(New DateTime(2017, 5, 23, 12, 16, 57, 0))
    
            ' check returned value
            Dim d2 As DateTime = GetVBdatetimeFromUnixTimestamp(i)
    
            ' your 'so called' integer value
            Dim d3 As DateTime = GetVBdatetimeFromUnixTimestamp(1495530867752)
    
        Function GetVBdatetimeFromUnixTimestamp(UnixTimeStamp As Double) As DateTime
            Dim UnixEpoch As New DateTime(1970, 1, 1, 0, 0, 0, 0)
            Return UnixEpoch.AddMilliseconds(UnixTimeStamp)
        End Function
        Private Function GetUnixTimestampFromVBdatetime(ByVal d As DateTime) As Double
            Dim ts As TimeSpan = (d - New DateTime(1970, 1, 1, 0, 0, 0, 0))
            Return ts.TotalMilliseconds
        End Function


    Regards Les, Livingston, Scotland

    Tuesday, May 23, 2017 11:11 AM
  • forget the integer.

    what about How can I convert this date to timestamp?

    Tue, 23 may 2017 12:16:57 GMT

    thank you

    Hi

    OK, try some of this:

            ' your date as provided
            Dim i As Double = GetUnixTimestampFromVBdatetime(New DateTime(2017, 5, 23, 12, 16, 57, 0))
    
            ' check returned value
            Dim d2 As DateTime = GetVBdatetimeFromUnixTimestamp(i)
    
            ' your 'so called' integer value
            Dim d3 As DateTime = GetVBdatetimeFromUnixTimestamp(1495530867752)
    
        Function GetVBdatetimeFromUnixTimestamp(UnixTimeStamp As Double) As DateTime
            Dim UnixEpoch As New DateTime(1970, 1, 1, 0, 0, 0, 0)
            Return UnixEpoch.AddMilliseconds(UnixTimeStamp)
        End Function
        Private Function GetUnixTimestampFromVBdatetime(ByVal d As DateTime) As Double
            Dim ts As TimeSpan = (d - New DateTime(1970, 1, 1, 0, 0, 0, 0))
            Return ts.TotalMilliseconds
        End Function


    Regards Les, Livingston, Scotland

    Hi LES,
    first question resolved form timestamp number I have my date now thanks to you

    1495530867752
      
     Dim format As String = ("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'")
    
    Dim d3 As DateTime = GetVBdatetimeFromUnixTimestamp(1495530867752)
            TextBox1.Text = d3.ToString(format)
    
     Function GetVBdatetimeFromUnixTimestamp(UnixTimeStamp As Double) As DateTime
            Dim UnixEpoch As New DateTime(1970, 1, 1, 0, 0, 0, 0)
            Return UnixEpoch.AddMilliseconds(UnixTimeStamp)
        End Function
    
    
    result is my date:
    Tue, 23 may 2017 09:14:27 GMT

    But now have to resolve the second question from my date to timestamp number

    from this-->

    Tue, 23 may 2017 09:14:27 GMT

    to my integer number posted before 

    1495530867752

    the second function give me again a date and not a number.

    Thank you for your help

    Tuesday, May 23, 2017 12:16 PM
  • I think code does not consider the last valut GMT

    because give me another number

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim i As Double = GetUnixTimestampFromVBdatetime(New DateTime(2017, 5, 23, 12, 16, 57, 0))
            TextBox2.Text = i
        End Sub
    
        Private Function GetUnixTimestampFromVBdatetime(ByVal d As DateTime) As Double
            Dim ts As TimeSpan = (d - New DateTime(1970, 1, 1, 0, 0, 0, 0))
            Return ts.TotalMilliseconds
        End Function
    result is : 1495541817000  and not the correct one 1495530867752

    Tuesday, May 23, 2017 1:02 PM
  • Nice puzzle. 

    For that I did not look at the Giro, but I think I got it.

    Module Module1
        Sub Main()
            Dim UnixTimeStampNow = New DateTime(2017, 5, 23, 9, 14, 23, 752).Subtract(New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds
            Console.WriteLine(UnixTimeStampNow)
            Console.ReadKey()
        End Sub
    End Module

    Thanks to Les, who taught me it are milliseconds.


    Success
    Cor

    Tuesday, May 23, 2017 1:08 PM
  • I think code does not consider the last valut GMT

    because give me another number

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim i As Double = GetUnixTimestampFromVBdatetime(New DateTime(2017, 5, 23, 12, 16, 57, 0))
            TextBox2.Text = i
        End Sub
    
        Private Function GetUnixTimestampFromVBdatetime(ByVal d As DateTime) As Double
            Dim ts As TimeSpan = (d - New DateTime(1970, 1, 1, 0, 0, 0, 0))
            Return ts.TotalMilliseconds
        End Function
    result is : 1495541817000  and not the correct one 1495530867752

    Hi

    Does this help. You need to consider the millisecond part of the various date/times used.

        Dim d As DateTime = GetVBdatetimeFromUnixTimestamp(1495530867752)
    
            Dim dp1 As Integer = d.Year         ' 2017
            Dim dp2 As Integer = d.Month        ' 5 (May)
            Dim dp3 As Integer = d.Day          ' 23
            Dim dp4 As Integer = d.Hour         ' 9
            Dim dp5 As Integer = d.Minute       ' 14
            Dim dp6 As Integer = d.Second       ' 27
            Dim dp7 As Integer = d.Millisecond  ' 752
    
    
            ' need to include the millisecons if you want
            ' to match up
            Dim d2 As DateTime = New DateTime(2017, 5, 23, 9, 14, 27, 752)
            ' this returns 1495530867752
    
    


    Regards Les, Livingston, Scotland

    Tuesday, May 23, 2017 2:14 PM