Home › Forums › General discussion › Resource Management & Calculated java script start & end date/time
- This topic has 6 replies, 2 voices, and was last updated 4 years, 5 months ago by Alexander Bautz.
-
AuthorPosts
-
-
June 10, 2020 at 16:23 #30574
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:30var d1 = new Date();
d.setHours(d2, d3, 0);
setFieldValue(“StartDate”)
return true;
}’Your help would be very much appreciated.
Regards
Paul
-
June 11, 2020 at 12:49 #30595
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
-
June 12, 2020 at 11:16 #30621
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:
-
June 12, 2020 at 12:39 #30626
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
- This reply was modified 4 years, 5 months ago by Alexander Bautz.
-
June 12, 2020 at 14:29 #30638
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
-
June 12, 2020 at 14:42 #30640
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
-
June 12, 2020 at 18:45 #30644
I’m glad you got it sorted out.
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.