1. Black Friday Sale - 50% off

    B4A Standard for $29 instead of $59!
    Buy Now
    Dismiss Notice

Android Question Now convert OLE-date(double) in B4A date format?

Discussion in 'Android Questions' started by AlexMaa, Apr 21, 2015.

  1. AlexMaa

    AlexMaa Member Licensed User

    How to convert the date of receipt of the OLE- automation.
    Need an analog functions and FromOADate ToOADate

    ---------
    in MS vb.net there is
    Returns object DateTime, an equivalent set, choose OLE- automation.
    ---------
    Dim instance As DateTime
    Dim returnValue As Double
    returnValue = instance.ToOADate ()
    DateTime.FromOADate (122.11)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you post an example of a date / time string and the ole value?
     
  3. DonManfred

    DonManfred Expert Licensed User

    Google-findings:

    Code:
    double d = 40880.051388 ;
    DateTime dt = DateTime.FromOADate(d);
    The value is an offset in days from December 30th, 1899. So you want:
    Code:
    new DateTime(18991230).AddDays(40880.051388)
    Also found this:
     
    Last edited: Apr 21, 2015
    tdocs2 and Peter Simpson like this.
  4. AlexMaa

    AlexMaa Member Licensed User

    There is an application server. It cannot be changed.
    The server sends data -> time values in the OLE format.
    In the app (Android) receive data - now they need to convert.
    I need a function to convert times from OLE in B4A date format.

    please need help


    ----------
    OLE -> Date
    42119.1810185185 = 04-25-2015 4:20:40
    12119.18 = 03-06-1933 4:19:12
    ----------
    in VB6
    CDate(42119,1810185185)
    CDbl(CDate(04-25-2015 4:20:40))

    ----------
    http://www.fxcodebase.com/documents/IndicoreDev/cpp/oledate.html
    https://msdn.microsoft.com/en-us/library/system.datetime.tooadate.aspx
    http://blogs.msdn.com/b/oldnewthing/archive/2003/09/05/54806.aspx
     
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. DonManfred

    DonManfred Expert Licensed User

    The code i found was a c# code if i remember correctly
    I´m not sure if that works (not tried)

    i posted this too in the hidden hope than you, @Erel maybe can find a way as if i remember correctly b4a is written in c# so you should be able to use the code i posted.

    I´m not aware of an solution. I just did a little bit google-search.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    I see. I missed that. I will try to convert it.
     
    DonManfred likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    This code works:
    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Log(DateUtils.TicksToString(OleDateToTicks(42119.1810185185)))
       
    Log(DateUtils.TicksToString(OleDateToTicks(12119.18)))
    End Sub

    Sub OleDateToTicks(ole As Double) As Long
       
    Dim origin As Long = DateUtils.SetDate(18991230)
       origin = 
    DateTime.Add(origin, 00Floor(ole))
       origin = origin + 
    DateTime.TicksPerDay * (ole - Floor(ole))
       
    Return origin
    End Sub
    However there are several assumptions here about the time zone (which you didn't provide any information about).
     
    tdocs2, AlexMaa and DonManfred like this.
  9. AlexMaa

    AlexMaa Member Licensed User

    Thank you
    ---------
    there is small problem - but this not critical

    42119.181015 = 04/25/2015 04:20:39
    VB Return
    42119.181015 = 04-25-2015 4:20:40
    ---------
    (ole - Floor(ole)) = 0.18101500000193482

    calculation double calls inaccuracy at one second
    (not always)
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    origin = Round(origin + DateTime.TicksPerDay * (ole - Floor(ole)))
     
  11. AlexMaa

    AlexMaa Member Licensed User

    Thank you, Erel

    What about the inverse function?
    Sub TicksToOleDate(ticks As Long) As Double
     
    Last edited: Apr 28, 2015
  12. Erel

    Erel Administrator Staff Member Licensed User

    This code is correct if you assume that all the time instances are GMT times:
    Code:
    DateTime.SetTimeZone(0)
    Log(TicksToOleDate(OleDateToTicks(42119.1810185185)))
    Log(TicksToOleDate(OleDateToTicks(12119.18)))


    Sub TicksToOleDate(ticks As Long) As Double
       ticks = ticks - DateUtils.SetDate(
    18991230)
       
    Return ticks / DateTime.TicksPerDay
    End Sub

    Sub OleDateToTicks(ole As Double) As Long
      
    Dim origin As Long = DateUtils.SetDate(18991230)
      origin = 
    DateTime.Add(origin, 00Floor(ole))
      origin = origin + 
    DateTime.TicksPerDay * (ole - Floor(ole))
      
    Return origin
    End Sub
     
    tdocs2 and AlexMaa like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice