How to convert DateTime (in ticks?) to hh:mm:ss

Hi guys,
How can I convert DateTime string (I think in ticks resulting from setting TraceOptions.DateTime) to a 'date' object that I can manipulate using Format?

/Michael
gem56Asked:
Who is Participating?
 
AkisCConnect With a Mentor Commented:
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001.

-I think that this will work-
Dim dd As String = timestamp_to_date(1245205371753)
        MsgBox(dd)
    Function timestamp_to_date(ByVal ts As Single) As String
        Dim ts_temp As Single = ts / 1000
        Return (DateAdd("s", ts_temp, "01/01/0001 00:00:00 AM"))
    End Function
0
 
Wayne Taylor (webtubbs)Commented:
Hi Michael,

To convert Ticks to a DateTime, use something like this....

        Dim TickStr As String = "633275309832187500"
        Dim ConvDate As New Date(Convert.ToInt64(TickStr))
        MsgBox(ConvDate)

Regards,

Wayne
0
 
gem56Author Commented:
Hi Wayne,
Thanks for your suggestion although it doesn't solve my problem but. I don't think it's your suggestion but rather the value that I'm trying to convert.

I got the value from a Trace.TraceInformation initiated output having TraceOptions set to DateTime. According to online Help manual that should give me DateTime value (e.g. Timestamp=964103455510), however that value gives me '2/01/0001 2:46:50 AM', which is a bit off.  ;-)

Any more suggestions?

/Michael
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
AkisCCommented:
If I understand well you want to convert the current timer to string

      Dim iT As Long = 0
        iT = Microsoft.VisualBasic.DateAndTime.Timer
        MsgBox(iT)
        Dim str_iT As String = TimeString(iT)
        MsgBox(str_iT)


    Public Function TimeString(ByVal xSeconds As Long) As String
        Dim sAns As String = ""
        '
        Dim lHrs As Long = 0, lMinutes As Long = 0, lSeconds As Long = 0
        lSeconds = xSeconds
        '
        lHrs = CType((lSeconds / 3600), Integer)
        lMinutes = (CType(lSeconds / 60, Integer)) - (lHrs * 60)
        lSeconds = CType(lSeconds Mod 60, Integer)
        '
        If lSeconds = 60 Then
            lMinutes = lMinutes + 1
            lSeconds = 0
        End If
        '
        If lMinutes = 60 Then
            lMinutes = 0
            lHrs = lHrs + 1
        End If
        If lMinutes < 0 Then
            lHrs = lHrs - 1
            lMinutes = 60 + lMinutes
        End If
        '
        sAns = Microsoft.VisualBasic.Strings.Right("00" & CType(lHrs, String), 2) & ":" & _
        Microsoft.VisualBasic.Strings.Right("00" & CType(lMinutes, String), 2) & "." & _
        Microsoft.VisualBasic.Strings.Right("00" & CType(lSeconds, String), 2)
        '
        Return sAns
    End Function
0
 
gem56Author Commented:
To be honest I'm not sure what I'm converting!
I just did a trace (at 17:25, 9th Oct local time) and got a Timestamp=1245205371753.
I don't know if that's seconds since 1900, 1970, etc. All that help manual says is that I can enable DateTime timestamp from being recorded in the trace.

Any further thoughts?
0
 
AkisCCommented:
-see if that works as you intent to-
   Dim dd As String = timestamp_to_date(1245205371753)
        MsgBox(dd)
    Function timestamp_to_date(ByVal ts As Single) As String
        Dim ts_temp As Single = ts / 1000
        Return (DateAdd("s", ts_temp, "01/01/1970 00:00:00 AM"))
    End Function

With the Timestamp there is UTC time involved, and you must know your_CultureInfo and the source_of_the_timestamp_CultureInfo to make a true convertion. Not friendly to vb.net users I beleive...
-read this article-
http://support.microsoft.com/kb/932955
0
 
gem56Author Commented:
Thanks for that AkisC. I'll take a look as soon as I get 5 min.
/Michael
0
 
gem56Author Commented:
I've tried a few things without any luck.
If I subtract 1/8/1951 I get something close to correct date however that date doesn't make sense and it also doesn't work for all values.
 I read the article and that seems to deal with adjustment dealing with daylight saving, etc. but doesn't seem to address my issue.

Any more ideas?

/Michael
0
 
AkisCCommented:
Could you give the link of the
"According to online Help manual that should give me DateTime value" as you wrote in the begining?
Just to give it a last try!
0
 
gem56Author Commented:
0
 
gem56Author Commented:
Hi AkisC,
I must be doing something wrong here. I'll give you an example of two trace messages with message time using Now function, the Timestamp that Trace gave me and the translation using timestamp_to_date function.

Using DateAdd("s", ts_temp, "01/01/0001 00:00:00 AM") in timestamp_to_date funtion I get the following result:
Message: Now=15/10/2007 10:13:54 AM --> Timestamp=2513405287161 --> timestamp_to_date=17/06/0040 2:22:56 AM
Message: Now=15/10/2007 10:39:08 AM --> Timestamp=2535093945341 --> timestamp_to_date=17/06/0040 2:22:56 AM

It appears that for a time difference of 25min 14sec (i.e. 1514 seconds) I got a difference of 21,688,935,833 ticks? meaning approximately 14,325,585 ticks?/sec which doesn't correspond to anything significant that I'm aware of.

Does that make any sense to you/anyone?

/Michael
0
 
AkisCCommented:
Sorry but I can not make out anything!
Maybe you should close this question and open another one.
It has many comments and I suppose a few if none will try to give you an answer

Take care
0
 
gem56Author Commented:
Hi AkisC, the question is unresolved but I'll give you the points for all your effort AkisC.
/Michael
0
 
AkisCCommented:
Thank you
Michael
this is what I do also :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.