B4J Question [B4X] Convert Day of the Year to Date

aminoacid

Active Member
Licensed User
Longtime User
I'm trying to convert from "day of the year" to a date. For example if the day of the year is 221 the date would be August 10, 2022 (or 08/10/2022 in mm/dd/yyyy format). I came up with the following sub to do this. It works, but is extremely slow to a point where if it is called multiple times very quickly, it crashes at the "DateParse" function.

Does anyone have a better and/or more elegant suggestion on how to do this? Without using DateParse() if possible?


B4X:
Sub GetDate (days As Long) As String
    Dim tmp As String
    Dim Tics As Long
    tmp="01/01/" & DateTime.GetYear(DateTime.Now)        ' Jan 1 of this year
    Tics = DateTime.DateParse(tmp)                                      ' Ticks to beginning of this year
    Tics = Tics + (days * 86400000)                                        ' Add tics to days in msec
    Return(DateTime.Date(Tics))
End Sub
 
Last edited:

TILogistic

Expert
Licensed User
Longtime User
?
DateUtils
B4X:
Sub GetDate2 (days As Int) As String
    DateTime.DateFormat = "dd/MM/yyyy"
    Dim day As Long = DateUtils.SetDate(DateTime.GetYear(DateTime.Now), 1, 1)
    Dim p As Period
    p.Days = days
    Dim new As Long = DateUtils.AddPeriod(day, p)
    Return DateTime.Date(new)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…