Resource Management & Calculated java script start & end date/time

Forums General discussion Resource Management & Calculated java script start & end date/time

Viewing 6 reply threads
  • Author
    Posts
    • #30574
      Paul Woodcock
      Participant

      Hi Alexander,

      I’m looking at using the resource manager and I’m hoping to set some predefined booking time-slots. E.g. 13:15, 14:00, 14:45 etc.

      I’m trying to combine a date/time field using ‘EventDate'(date/time field) and ‘TimeSlot’ (string field from a cascading dropdown linked to the list). Then output to a new date/time field called ‘StartDate’. This would then allow me to use the ‘StartDate’ field as the Start Date in the resource manager.

      I’m planning on doing something similar for the end date as well, by adding 45 minutes onto the StartDate. And another variable to double or triple the end time.

      This is the javascript but I keep going wrong:

      `function dffs_PreSaveAction() {
      var d1 = date.toString(spjs.utility.getDateFieldAsDateObject(“EventDate”)).slice(0,10);
      var d2 = date.toString(spjs.utility.getField(“TimeSlot”)).slice(0,2);// 24 hours string field e.g. 10:30
      var d3 = date.toString(spjs.utility.getField(“TimeSlot”)).slice(4,5);// 24 hour string field e.g. 10:30

      var d1 = new Date();
      d.setHours(d2, d3, 0);
      setFieldValue(“StartDate”)
      return true;
      }’

      Your help would be very much appreciated.

      Regards

      Paul

    • #30595
      Alexander Bautz
      Keymaster

      Hi,
      You can do it something like this:

      var date = spjs.utility.getDateFieldAsDateObject("EventDate");
      var timeSlot = getFieldValue("TimeSlot").split(":");
      var hour = timeSlot[0];
      var minutes = timeSlot[1];
      date.setHours(hour, minutes, 0, 0);
      spjs.utility.setDateFieldFromDateObject("StartDate", date);

      Alexander

    • #30621
      Paul Woodcock
      Participant

      Hi Alexander,

      Thank you for your help. The code works for setting the minutes but there is a problem setting the hour in the StartDate field (see attachment). Its a calendar type SharePoint app.

      The only this I can think of why its failing is because in the hours dropdown show 08:, 09:, 10:, 11: by default. Is the “:” causing an problem?

      If I try to append the “:” to the hour it causes an error as its not a recognised date format.

      Regards

      Paul

      Attachments:
    • #30626
      Alexander Bautz
      Keymaster

      The calendar list template differs from the other templates and don’t use military time (00-23) in the hour select value attribute, but uses 12 AM – 11 PM or as in your case appends : to the hour. The setDateFieldFromDateObject function don’t take this into account I’m afraid.

      This means you must change this line:

      spjs.utility.setDateFieldFromDateObject("StartDate", date);

      like this:

      setFieldValue("EventDate", [(date.getMonth() + 1)+"/"+date.getDate()+"/"+date.getFullYear(), hour, minutes]);

      Alexander

    • #30638
      Paul Woodcock
      Participant

      Hi Alexander,

      That worked like a charm. Its just displaying the date in US date format mm/dd/yyyy rather than UK date format.

      I’ve tried adding:

      spjs.dffs.data.lcidToDateFormat["2057"] = "d/m/y";

      before the function but this did not alter the format in the form.

      Paul

    • #30640
      Paul Woodcock
      Participant

      Hi Alexander,

      I’ve worked it out, I just needed to rearrange your line of code:

      setFieldValue("EventDate", [date.getDate()+"/"+(date.getMonth() + 1)+"/"+date.getFullYear(), hour, minutes]);

      When I tried this earlier I must of mistyped something.

      Thanks again,

      Paul

    • #30644
      Alexander Bautz
      Keymaster

      I’m glad you got it sorted out.

      Alexander

Viewing 6 reply threads
  • You must be logged in to reply to this topic.