This project has moved. For the latest updates, please go here.

set second time date field on change

May 16, 2016 at 12:55 PM
Edited May 16, 2016 at 12:56 PM
I have two time/date fields and would like the second field to display the same date as the first when the first is changed.

I've used the following previously (below) to set two fields the same, and have tried to modify it to suit the new requirement, but I'm not having much luck..
     var statusFieldValue = SPUtility.GetSPFieldByInternalName('ActiveTitle');

     $(statusFieldValue.Dropdown).on('change', function() { 

        var ActivityFieldValue = SPUtility.GetSPFieldByInternalName('ActiveTitle').GetValue();
            SPUtility.GetSPFieldByInternalName('Activity').SetValue(ActivityFieldValue);
}) 
below is the modified code for the date time field
     var DateFieldValue = SPUtility.GetSPFieldByInternalName('Start_x0020_Time');

     $(DateFieldValue .Dropdown).on('change', function() { 

            SPUtility.SetTimeFormat('24HR');
            var DateFieldValue =                SPUtility.GetSPFieldByInternalName('Start_x0020_Time').GetValue();
        SPUtility.GetSPFieldByInternalName('End_x0020_Time').SetValue(DateFieldValue);
                        
        }); 
        
There seems to be two problems,;

the (DateFieldValue .Dropdown).on('change' doesnt work.
Maybe because the DateFieldValue isn't a dropdown?


SPUtility.GetSPFieldByInternalName('End_x0020_Time').SetValue(DateFieldValue);
doesn't seem to set the end time to the start time.
but it will set a standard text field to a date, e.g. "16/05/2016 13:00"
Coordinator
May 18, 2016 at 2:38 AM
Edited May 18, 2016 at 2:40 AM
I tested this out myself and looks like you are right.

For the first question, Date/time fields do not have a Dropdown property. Instead, they have the DateTextbox, HourDropdown, and MinuteDropdown properties where the Hour/Minute dropdown fields are null when the field is a date only field. I tested it out and you can monitor the change event for these three. For example:
var f = SPUtility.GetSPField("Date and Time");
$(f.DateTextbox).change(function() {
   console.log("date field changed");
});
// same thing for HourDropdown and MinuteDropdown
However, this doesn't work when the user clicks the calendar icon and selects a date using the calendar dialog. We'll need to do some more research on this to see if we can trap this event as well.

For the second question, makes sense what you are trying to do and I had an "oh duh" moment when I saw it. I've logged an enhancement https://github.com/kitmenke/sputility/issues/33. Right now the SetValue function for Date/Time fields takes each parameter individually like field.SetValue(year, month, day, hour, minute). So as a workaround you can do this instead:
var start = SPUtility.GetSPFieldByInternalName("Start_x0020_Time").GetValue();
SPUtility.GetSPFieldByInternalName("End_x0020_Time").SetValue(start.Year, start.Month, start.Day, start.Hour, start.Minute);
Edit: Sorry lots of typos.. hopefully fixed now.