Android Question Get date from day number

Solution
Have you tried use AddPeriod (from DateUtils) with number of days minus 1 from 1/1/2022 ?
Have you tried use AddPeriod (from DateUtils) with number of days minus 1 from 1/1/2022 ?
 
Upvote 1
Solution
Calculate Date for 1st january

Add a Period with 171 days to this date to get the new date.


OR use this one directly
B4X: Copy
dim newdate as long = DateTime.Add(datevalue1stjanuary, 0, 0, 171)
 
Upvote 0
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
This is what I got. Working great so far

B4X: Copy
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
Here is another Variation that does not depend on DateUtils

B4X: Copy
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
Back
Top