[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-08
14
Medium Priority
?
1,332 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:gem56
  • 7
  • 6
14 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20038311
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
 

Author Comment

by:gem56
ID: 20038376
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
 
LVL 11

Expert Comment

by:AkisC
ID: 20039044
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:gem56
ID: 20039085
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
 
LVL 11

Expert Comment

by:AkisC
ID: 20039185
-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
 

Author Comment

by:gem56
ID: 20047001
Thanks for that AkisC. I'll take a look as soon as I get 5 min.
/Michael
0
 

Author Comment

by:gem56
ID: 20055402
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
 
LVL 11

Expert Comment

by:AkisC
ID: 20055973
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
 
LVL 11

Accepted Solution

by:
AkisC earned 750 total points
ID: 20058220
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
 

Author Comment

by:gem56
ID: 20075380
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
 
LVL 11

Expert Comment

by:AkisC
ID: 20076328
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
 

Author Comment

by:gem56
ID: 20076420
Hi AkisC, the question is unresolved but I'll give you the points for all your effort AkisC.
/Michael
0
 
LVL 11

Expert Comment

by:AkisC
ID: 20076453
Thank you
Michael
this is what I do also :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question