This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

From Julian day to date
#1
Ciao to all,

I've made a small script that convert back the julian day to the specific date.

Using the 
Code:
local cDate = os.date('*t') 

you can get the cDate.yday that represent the julian day (counting 1 from Jan 1st)

Apparentely there is no backformula that giving the year and the day, returns the complete date...

Well, I've made it:

Code:
function j2d(yd, aaaa)
  local mm, dd = 1, 1
  local cDate  = os.date('*t')
  local mdays  = {31,28,31,30,31,30,31,31,30,31,30,31}
  if not aaaa then aaaa = cDate.year end
  if aaaa % 4 == 0 then mdays[2] = 29 end
  for m = 1, 12 do
    if mdays[m] > yd then
      yd = yd - mdays[m]
      mm = m
    else
      dd = yd
      break
    end
  end
  yDate   = os.date('*t', os.time({year = aaaa, month = mm, day = dd}))
  yString = os.date('%d/%m/%Y',os.time({year = aaaa, month = mm, day = dd}))
  return yString, yDate
end

this functin can be saved into the common user function and be called by:
Code:
local cDate, tDate = j2d(270, 2022)

this will return the string for the date and the os.date('*t') table for the selected day...

hope this would help you somehow
ciao
M
Reply


Messages In This Thread
From Julian day to date - by emme - 04.10.2022, 14:31

Forum Jump: