0

Incrementing date in a script

I am setting up a database for my teaching. I'm trying to create a button that, when pressed, creates a new lesson with the same class but increments the date by 1 or 2 weeks. Currently I can get it to duplicate the date exactly but when trying to add the time interval selected the script fails:

let myN := 'Time interval';
let myDate := Appointment;
let myClass := Class;
let myComp := 'Composite Class';
let new := (create 'Lesson Schedule');
new.(Appointment := myDate + myN);
new.(Class := myClass);
new.('Composite Class' := myComp)

As you can see it is "myDate + myN" that isn't working properly... "myDate" alone works perfectly fine so I'm clearly not understanding something about date/time functions.

Can anyone help? Thanks a lot,

Jack

12 replies

null
    • Jack_Mackenzie
    • 4 yrs ago
    • Reported - view

    Ideally I would like this to loop until it meets a date specified in an "End Date" field, if anyone can suggest a way of doing that as well, so that all the lessons are filled out for a whole term with one button press. I have no idea where to start...

    • Sean
    • 4 yrs ago
    • Reported - view

    If you want to increment a date by a week, you can just add 7 to the date or some multiple of 7 and I would use a Number field instead of a Time interval field. You can use a while-loop to iterate through the dates until the "End Date". Something like this...

     

    let new := (create 'Lesson Schedule');

    while myDate < 'End Date' do

    myDate := myDate + 7;

    new.(Appointment := myDate);

    new.(Class := myClass);

    new.('Composite Class' := myComp)

    end

     

    You might want to add a counter to the while-loop just as a precaution to avoid an infinite loop. Something like this...

     

    while myDate < 'End Date' and count < 10 do

     

    Be sure to increment the counter inside the loop.

    • Sean
    • 4 yrs ago
    • Reported - view

    Hopefully you already figured this out...

     

    let new := (create 'Lesson Schedule');

     

    ...needs to be inside the loop. I was using a variable instead of creating records when I was testing. Sorry for any confusion.

    • Jack_Mackenzie
    • 4 yrs ago
    • Reported - view

    Thanks for the info regarding looping the script. There's still a problem with incrementing the date - the moment I try to add anything to the appointment, whether it's a time interval or number, the script stops working. Are calculations working properly with appointment fields or should I do some alternative using date/time?

    • Sean
    • 4 yrs ago
    • Reported - view

    Well, the title did say "Incrementing date in script" so I assumed it was a Date field ;-). I haven't spent much time working with the Appointment field, but it does function differently than the Date field. If you don't need the Appointment field type then I would change it to Date and it should work. If you haven't looked at the "Reference of NX Scripting Functions" page you might find it helpful also.

     

    https://ninoxdb.de/en/manual/calculations/reference-of-functions-and-language

    • Jack_Mackenzie
    • 4 yrs ago
    • Reported - view

    Sorry for the ambiguity and thanks again for your help! I believe I do need the appointment type as I want it to show in the calendar and give each lesson a specific length - would there be a way of doing this with date/time or am I stuck with appointment? The manual states that appointments work in the same way as dates but it just doesn't seem to be working here... clearly there's something I've missed.

    • Sean
    • 4 yrs ago
    • Reported - view

    You need to use milliseconds to modify an Appointment value...

     

    let myN := 86400000 * 7;
    start(Appointment) + myN

    • Jack_Mackenzie
    • 4 yrs ago
    • Reported - view

    Thank you. Still nothing worked with applying functions to the appointment field directly so with your millisecond clue I've ended up converting the appointment into timestamp+duration then converting back, which works properly but seems a bit long winded. I'd be interested to know if there was any cleverer way of doing it but this is working for now:

     

    let myStart := start(Appointment);
    let myDura := duration(Appointment);
    let myN := 'Repeat on Nth day' * 86400000;
    let myClass := Class;
    let myComp := 'Composite Class';
    let newStart := myStart + myN;
    let myNew := appointment(newStart, myDura);
    let new := (create 'Lesson Schedule');
    new.(Appointment := myNew);
    new.(Class := myClass);
    new.('Composite Class' := myComp)

    • Sean
    • 4 yrs ago
    • Reported - view

    Jack, Glad you got it working. The only time I work with Appointment fields is when I'm trying to help someone on the forum. Since you wanted to use a loop to create new records with appointments, you needed to do what you posted. It's automated now so it's saving you some time and effort :-).

    • Jack_Mackenzie
    • 4 yrs ago
    • Reported - view

    Thanks a lot Sean. I wonder if you or someone else can shed light on a couple of things that have arisen now it's working. They may be more in depth than a text discussion will allow and if so I'll leave it and work around them manually.

     

    Here is the script so far.

    let myClass := Class;
    let myComp := 'Composite Class';
    let myStart := start(Appointment);
    let myDura := duration(Appointment);
    let myFin := datetime(Until);
    let myRep := 'Repeat on Nth week';
    let myCount := 0;
    let myN := myRep * 7 * 86400000;
    while myStart + myN < myFin and myCount < 10 do
    myCount := myCount + 1;
    myStart := myStart + myN;
    let myNew := appointment(myStart, myDura);
    let new := (create 'Lesson Schedule');
    new.(Appointment := myNew);
    new.(Class := myClass);
    new.('Composite Class' := myComp);
    new.('Repeat on Nth week' := myRep)
    end

    First issue: This works as desired until the clocks go forward or backwards for British Summer Time! At which point due to Appointments using timestamps the appointment times are also moved! Any idea how I might be able to accommodate this within the script above?

     

    Second issue: I would like to include holidays so that the loop skips any days that are indicated as holidays and resumes thereafter. I feel that might be a fair amount of extra work but any pointers or ideas would be great.

     

    Thirdly: I'm basically completely new to coding beyond a bit of BASIC when I was about 10 - if there are any schoolboy errors in what I've done I'd love to correct them.

     

    Jack

    • Sean
    • 4 yrs ago
    • Reported - view

    Jack, Here are a couple of links that may help...

     

    https://ninoxdb.de/en/forum/technical-help-5ab8fe445fe2b42b7dd39ee7/date-fields-and-timezones-issue-unix-vs-utc-conflict-5b7462cc03f12310870ad569?post=5b7462cc03f12310870ad56a&page=0

     

    https://ninoxdb.de/en/forum/use-cases-5abd0b9c4da2d77b6ebfa395/how-to-account-for-holidays-when-calculating-workdays-5b871561d11f6f5f857bdfe1

     

    As far as your code goes, I don't see anything wrong so you must have been a good student ;-). I find the Ninox scripting language easy to use and it has some powerful functions.

    • Jack_Mackenzie
    • 4 yrs ago
    • Reported - view

    Excellent! I shall look through those links carefully. Thank you :)

Content aside

  • 4 yrs agoLast active
  • 12Replies
  • 3086Views