Logic Machine Forum
From Julian day to date - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: From Julian day to date (/showthread.php?tid=4283)



From Julian day to date - emme - 04.10.2022

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