How to add 30 minutes to startdate in update/delete dialog in fullcalendar 3.10 javascript

In the update window I would like to automatically add 30 minutes to the event start date:

$('#updatedialog').dialog({
        autoOpen: false,
        width: 680,
        buttons: {
            "update": function () {

                var eventToUpdate = {
                    id: currentUpdateEvent.id,
                    title: $("#eventName").val(),
                    description: $("#eventDesc").val(),
                    color: $("#colorPicker").val(),
                    start: new Date($("#eventStart").val()),
                    end: new Date($("#eventEnd").val()),
                    //end: new Date($("#eventEnd").val(moment(event.start).add(30, "m").format("YYYY-MM-DD[T]HH:mm"))),
                    //end: new Date($("#eventEnd")).setMinutes($("#eventStart").val() +30),
                    note: $("#EditEventNote").val(),
                    /*start: moment($("#eventStart").val(), "DD/MM/YYYY HH:mm"),*/
                    
                   
                };
                
               
                PageMethods.UpdateEvent(eventToUpdate, updateSuccess);
                $(this).dialog("close");

                currentUpdateEvent.title = $("#eventName").val();
                currentUpdateEvent.description = $("#eventDesc").val();
                currentUpdateEvent.color = $("#colorPicker").val();
                currentUpdateEvent.note = $("#EditEventNote").val();
                currentUpdateEvent.start = new Date($("#eventStart").val());
                currentUpdateEvent.start.setMinutes(currentUpdateEvent.start.getMinutes() + 30);
                $('#calendar').fullCalendar('updateEvent', currentUpdateEvent);
                $('#calendar').fullCalendar('refetchEvents');         

            },

But this code doesn’t work.

Answer

Firstly this is largely a momentJS issue, not a fullCalendar one.

You need to clone the start date, mutate it by 30 minutes, and then format it for output.

N.B. event.start doesn’t actually appear anywhere in your code, so I’m assuming you actually wanted to use the value from the “eventStart” textbox instead. And I’ve gone back to setting the date format the way we agreed in your earlier question about this code. It’s unclear why you’ve stopped doing it that way, because you already made clear it didn’t work to set the start/end dates using new Date(...).

Therefore your code would look this like:

"update": function () {
  var start = moment($("#eventStart").val()); //parse the start date
  var end = moment(start); //clone the start date to make the end date
  end.add(30, "m"); //add 30 minutes to the end date

  //create the event object with formatted dates
  var eventToUpdate = {
    start: start.format("YYYY-MM-DD HH:mm"),
    end: end.format("YYYY-MM-DD HH:mm")
    //...etc
  };

  $("#eventEnd").val(end.format("YYYY-MM-DD[T]HH:mm")); //only include this line if you actually want to set the value of the eventEnd textbox.
  //...etc

Minimal demo: https://codepen.io/ADyson82/pen/PojKGXY

Relevant documentation:

  1. https://momentjs.com/docs/#/parsing/moment-clone/

  2. https://momentjs.com/docs/#/manipulating/add/


P.S. I want to make a more general point about your approach to programming, based on what you’ve shown here, and your various previous questions where I’ve intervened.

This attempt:

//end: new Date($("#eventEnd").val(moment(event.start).add(30, "m").format("YYYY-MM-DD[T]HH:mm")))

contains a jumble of code all packed together in what appears to be a giant piece of guesswork, or possibly a “throw everything at it until something sticks” approach. Neither of those techniques are going to make you a good programmer. Programming is a science / engineering discipline, not an art form or a hit-and-hope game. You need to be methodical and precise. If the best you can say about it is that it “doesn’t work”, then it’s clear you haven’t analysed it at all. To solve problems in your code, you need to analyse it and debug it piece by piece.

So what you need to do with that code, to see why it’s not working, is take it all apart, read the documentation properly for each of the many functions you’ve used there, and work out what they output and whether that’s relevant to your situation – and/or whether each one gives suitable input to the thing you’ve included it within.

When testing JavaScript / jQuery code you should also be checking in your browser’s Console (in the Developer Tools) for errors, and using the built-in debugger to step through the code line by line to see the values of your variables, and spot where the code and the values don’t do what you expected them to. (If you don’t know what you expect a particular part of the code to do, then you should either scrap that part and write something you can understand, or go and read documentation and try examples using that code until you do understand it. Otherwise you’re just back to guessing again, and that’s pointless.)

You can test each part of the code individually to verify your understanding. Putting large numbers of function calls together in one line like in the example above makes it very difficult to debug – for testing, you should split it into invidiual lines and see what each one produces, before deciding whether it’s useful to put into the next step. For example if we break down that code into separate steps, we’d end up with:

var moment1 = moment(event.start);
moment1.add("30", "m");
var formattedDate = moment1.format("YYYY-MM-DD[T]HH:mm");
var x = $("eventEnd").val(formattedDate);
var dt = new Date(x);
var eventToUpdate = {
 //...
 end: dt
}

Now let’s pick apart the logical flaws, one by one:

  1. event.start doesn’t appear to exist in the context you’ve shown (unless you’ve omitted some outer code). So you’re probably starting with a blank to begin with. As I mentioned above, I’m guessing you actually meant to use the value from the eventStart textbox instead.

  2. If event.start does exist in this context, then it looks like it probably came from fullCalendar, and fullCalendar v3 (as per its documentation) already gives you dates as momentJS objects – so you don’t need to wrap them in the moment() constructor all over again. Whilst it does no harm, it’s also inefficient and clutters up your code for no reason.

  3. $("eventEnd").val() will set the value of a field on the page. It’s not clear whether this was intentional or not, since your stated objective was to add 30 minutes to the event’s start date (presumably to form an end date ready to send to the server, although your wording could be interpreted to mean you actually want to modify the start date itself, not use it as an end date).

  4. .val() doesn’t return a value (it only sets a value when used the way you’ve done it). Therefore x will always be empty.

  5. Because x is empty, new Date(x) won’t do anything useful.

  6. You don’t want new Date anyway, because you need to send a pre-formatted Date as a string to your server – we already discussed this in one of your previous questions so it’s not clear why you’ve tried this again now – apart from, as I’ve assumed above – that you still don’t really understand any of these individual pieces of code and are still guessing randomly without testing them properly or reading anything about what they do. I will speak candidly and say that you will never achieve anything as a programmer until you learn to do both of those things. Once you start doing them, your tasks will suddenly start to seem a lot easier.

As you can see, once we break this down one by one, it’s a lot easier to find the problems on their own than when it’s all together in one line and you can’t tell which part is doing what. Once you’ve got something that works, then it’s fine to reduce it down into less lines if you think it saves space or makes the code neater. But don’t do that until you’re sure it’s going to do what you want.