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.

Change month and year in a time script
#1
Good companions, thanks for reading this thread. This is to tell you that I have one function per event which calls a user library. The idea is that by means of a button you can complete the fields of a date and a time variable based on previously established parameters, so that if it was set to "dia_tiem1" in 1 and "hor_tiem1" in 13 : 00: 00, automatically the value of 13:00:00 will be written in the "hour_checkout" variable and in the "day_checkout" variable the day following the current date will be written. The problem occurs is when the end of the month arrives, the ideal is that when executing the button on day 31 in the variable "dia_checkout" change the month and becomes September 1 for example, but it happens that it does not exceed 31 And neither does the month change. I imagine that this same problem I will have when it is December 31, the ideal would be that in the variable "dia_checkout" becomes January 1 of the following year, but this will not happen. I abbreviate the code that works for me as long as it is not 31 of the month and of which I have doubts about its behavior on the 30th and in February which has 28 days and 29 when it is leap. Thank you and I am attentive to your comments.

Code:
function perfil_fecha(obj_perf,hora_checkout,dia_checkout,dia_tiem1,hor_tiem1,dia_tiem2,hor_tiem2,dia_tiem3,hor_tiem3)
 
  local hor_dia = os.date('*t')
  if (obj_perf==1) then
    set_dia=hor_dia.day+dia_tiem1
    date_obj = {
        day = set_dia,
        month = hor_dia.month,
        year = hor_dia.year,
    }
    log(date_obj)
    grp.write(dia_checkout, date_obj)
    grp.write(hora_checkout, hor_tiem1)

        elseif (othercondition) then
          dosomethingelse()
    end

end
Reply
#2
You can use this function:
Code:
function shiftdate(date, days)
  date.hour = 12
  return os.date('*t', os.time(date) + days * 86400)
end

First argument is date table (with day/month/year data), second argument is number of days to add. This will handle month/year rollover correctly.
Reply
#3
Hi Admin, thanks for replying. I used the function, but it is not working for me, I don't know if it is because of my bad configuration of both the types of data or I am omitting something previous. I attach the error message, the object settings and the script by event. Thank you and I am attentive to your comments.

Script by event
Code:
require('user.perfil_horario')

date='32/0/23'
days=grp.getvalue('32/0/30')
shiftdate(date,days)
Funtion:

Code:
function shiftdate(date,days)
  date.hour = 12
  return os.date('*t', os.time(date) + days * 86400)
end

Log Error:

Event for Tiempo Check Out (32/0/24) 05.08.2019 10:55:21
User library perfil_horario:2: attempt to index local 'date' (a string value)
stack traceback:
User library perfil_horario:2: in function 'shiftdate'
User script:15: in main chunk

Attached Files Thumbnail(s)
   
Reply
#4
Hi

You have error in line 3, and you should split our result from the function to some variable.
Code:
function shiftdate(date,days)
  date.hour = 12
  return os.date('*t', os.time(date) + days * 86400)
end


date = grp.getvalue('date')
days=grp.getvalue('days')

log(shiftdate(date,days))
------------------------------
Ctrl+F5
Reply
#5
Hi Admin and Daniel, thanks for replying. With your help I could already do the Script. Thank you
Reply


Forum Jump: