Android Question Get date from day number

mcqueccu

Well-Known Member
Licensed User
Longtime User
As the title suggests, I will like to get a full date from a day number out of ~365 days in the year.
For example, when I enter day 171, it should get 2022-06-20
 
Solution
Have you tried use AddPeriod (from DateUtils) with number of days minus 1 from 1/1/2022 ?

aeric

Expert
Licensed User
Longtime User
Have you tried use AddPeriod (from DateUtils) with number of days minus 1 from 1/1/2022 ?
 
Upvote 1
Solution

mcqueccu

Well-Known Member
Licensed User
Longtime User
Thank you @aeric and @DonManfred . I also got the same idea from Exceljet website

Sometimes, something simple will just be staring you in the face, but you tend to overthink it.

At times, you will spend lot of time on something, as soon as you post in the forum, that's when the solution decides to pop up :cool:
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
This is what I got. Working great so far

B4X:
Sub getDateFromDayNumber (day As Int)  As String
    DateTime.DateFormat = "yyyy/MM/dd"
    Dim yr As Int = DateTime.GetYear(DateTime.Now)
    Dim p As Period
    p.Days = day - 1
    Dim nDate As Long = DateUtils.AddPeriod(DateTime.DateParse($"${yr}/01/01"$),p)
    Return DateTime.Date(nDate)
End Sub
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
Here is another Variation that does not depend on DateUtils

B4X:
Sub getDateFromDayNumber2 (day As Int)  As String
    DateTime.DateFormat = "yyyy/MM/dd"
    Dim yr As Int = DateTime.GetYear(DateTime.Now)
    Dim d As String = $"${yr}/01/01"$
    Dim nDate As Long = DateTime.Add(DateTime.DateParse(d),0,0,day - 1)
    Return DateTime.Date(nDate)
End Sub
 
Upvote 0
Top