Javascript format to update complex date

Working on a way to set the duration of a complex date to 30 minutes as part of this problem http://helpdesk.knackhq.com/support/discussions/topics/5000045039 and making some headway through the Javascript.

Now I'm stuck on how to set the field to the new value with this now working:

 

var NewData = {
    field_342: "3/27/2015 2:00pm to 2:30pm"
}

 I've tried all sorts of attempts at accessing field_342_raw including field_342_raw.to.date etc without success.

Any ideas?

Brad

Hello Knack community.

After a few days of beating my head against the wall - i thought i would post the proper syntax that ended up making an insert into a timer field possible.

 

field_YOUR-ID: {
     "times":[
          {
               "from":{
                    "date": "11/29/2018",
                    "hours": "11",
                    "minutes": "00",
                    "am_pm": "am"
               },
               "to":{
                    "date": "11/29/2018",
                    "hours": "11",
                    "minutes": "30",
                    "am_pm": "am"
               }
          }
     ]
}

 

the [ & ] were the key - hope that this helps someone else.

Hello all,

The CSS code below provided above hides the second date field for the Date but does not work for the Timer. Please  help. Thank you!

Serge

/* Hide to_date input field */

#kn-input-field_179 [name="to_date"] {visibility: hidden; width: 0px;} 

Thanks Greg, look forward to trying that for some other projects.

We ended up taking a different approach which was to control the inputs before records submission:

  1. CSS to hide the complex date controls & hide the second date field but keep the second time field.
  2. Javascript to set the second time field after the first was updated.

 

/* Hide complex date options */
#kn-input-field_1 > div {visibility: hidden; height: 0px;}
/* Hide to_date input field */
#kn-input-field_1 [name="to_date"] {visibility: hidden; width: 0px;}

 

// Function to return time result for appointments - 8:30am
function CalcEndTime(StartTime, DurationMinutes) {
var TimeStrRaw = new String(StartTime.replace('pm', ''));
TimeStrRaw = TimeStrRaw.replace('am', '')
var adjustHours = new Number;
var Hours = new Number;
var Minutes = new Number;
var am_pm = StartTime.substr(StartTime.length - 2);

if (StartTime.search('pm') == -1) { adjustHours = 0; } else { adjustHours = 12 }
var time = TimeStrRaw.split(':');

Hours = Number(time[0]) + adjustHours;
Minutes = Number(time[1]) + DurationMinutes;

if (Minutes > 59) {
    Minutes = Minutes - 60;
    Hours = Hours + 1;
}

var MinutesStr = new String(Minutes);
if (Minutes < 10) { MinutesStr = '0' + MinutesStr }

if (Hours > 12) { Hours = Hours - 12; }

if (Hours == 12) {
    if (am_pm = 'am') { am_pm = 'pm'; } else { am_pm = 'am'; }
}
if (Hours == 13) { Hours = 1; }

var TimeString = new String(Hours + ":" + MinutesStr + am_pm);
return TimeString;

}

$(document).on(‘knack-view-render.any’, function (event, view, data) {
// New appointment tests
$(‘input#view_775-field_342-time.kn-time.ui-timepicker-input’).change(function () {
$(‘input#view_775-field_342-time-to.kn-time.ui-timepicker-input’).attr(‘value’, CalcEndTime(this.value, 30));
});
});

  

Here's the snippet of code I finally figured out (thanks to many here) to do something similar.

What I wanted to do was create a new complex date based on a start date and an end date so that I could display the entry in a calendar view. I calculate the end date as an offset number of days from the start date.

  

$(document).on('knack-record-create.view_4', function(event, view, record) {

// use data from inserted record
console.log(record);
var data = {
field_2: { // complex date
date: record.field_4, // start date
to: {
date: record.field_5 // end date
}
}
}

// update the record
$.ajax({
url: “https://api.knackhq.com/v1/objects/object_1/records/” + record.id,
type: “PUT”,
headers: {“X-Knack-Application-Id”: “xxx”, “X-Knack-REST-API-Key”:“xxx”},
data: data,
success: function(response) {

  console.log('Record updated!');
}

});

});

  

Back working on this and still not getting anywhere, even using a 'from' and 'to' construct in the data with my variables like this.   Any other ideas?

 

var NewData = {
    field_434: 
    	{
      	"from":{
        	"date":StartTime.date,
        	"hours":StartTime.hours,
        	"minutes":StartTime.minutes,
        	"am_pm":StartTime.am_pm
      	},
      	"to":{
        	"date":EndTime.date,
        	"hours":EndTime.hours,
        	"minutes":EndTime.minutes,
        	"am_pm":EndTime.am_pm
      	}
    	}
  }

 

Thanks Nicholas,

Taking your suggestion I'm working on this data structure that's still failing:

  

var NewData = {
    field_342: { 
  		"date":"03/28/2015",
  		"hours":2,
  		"minutes":0,
  		"am_pm":"pm",
      	to:{
      		"date":"03/28/2015",
  			"hours":2,
  			"minutes":30,
  			"am_pm":"pm"
      	}
	}
  }

  Any other ideas?

Hi Brad,

Take a look at how the date / time field should be structured:

http://helpdesk.knackhq.com/solution/categories/5000053588/folders/5000083636/articles/5000446405-field-types#date_time

So it would be something like this:

field_342: { 

  "date":"03/27/2015",

  "hours":2,

  "minutes":30,

  "am_pm":"pm"

}

}

Hope this helps!