Extract and parse four parts of a complex date field for use in other fields

Comments

3 comments

  • Avatar
    brad preslar

    Javascript. I meant Javascript. Derp.

  • Avatar
    brad preslar

    updated code. This doesn't work yet, but I think I figured out how to parse the date field.

    // Update your view number to match your project
    $(document).on('knack-record-create.view_259', function(event, view, record) {
     
      var data = new Object(); 
      // Updated my view number, and field numbers field, trying to parse the data object.
      var data = ("#view_259-field_299");
      var getData = JSON.parse(data);
    
    alert(getData.date); //output: 02/08/2013
    alert(getData.hours); //output: 2
    alert(getData.minutes); //output: 0
    alert(getData.am_pm); //output: pm
    
    for(i=0;i<getData.to.length;i++)
    {
        alert(getData["to"][i].date); //output: 02/11/2013
        alert(getData["to"][i].hours); //output: 2
        alert(getData["to"][i].minutes); //output: 0
        alert(getData["to"][i].am_pm); //output: pm
    } 
           
      Knack.showSpinner();
      // This was originally a call that added the complex date to the database. But I need it to add the new simplified dates to the database.
      // You will need to update with your own <Application ID> and <API Key>
      // Also update with the correct scene and view numbers.
      $.ajax({
        url: "https://api.knackhq.com/v1/scenes/scene_135/views/view_259/records/" + record.id ,
        type: "PUT",
        headers: {"X-Knack-Application-Id": "<Knack-Application-ID>", "X-Knack-REST-API-Key":"<API-Key>"},
        field_275 : Data.date,
    	field_276 : Data.hours":"Data.minutes" "Data.am_pm,
    	field_277 : Data.to.date,
    	field_278 : Data.to.hours":"Data.to.minutes" "Data.to.am_pm,
        success: function(response) { 
            console.log("Record Updated with this data:");
            console.dir(data);
        },
        error: function(response) {
          alert('Update Failed!');
          console.log( response.error );
        },
        complete: function () {
          Knack.hideSpinner();
        }
      });
    });

     

  • Avatar
    brad preslar

     I think I'm getting closer, but am getting unexpected values in the JSON.

    $(document).on('knack-record-create.view_222', function(event, view, record) {
        
      // use data from inserted record
    	
    
    console.log(record);
    
    	var enteredDate = $("#view_222-field_302_raw").val();
    	
    	var getData = JSON.parse( enteredDate );
    
    alert(getData.date); //output: 02/08/2013
    alert(getData.hours); //output: 2
    alert(getData.minutes); //output: 0
    alert(getData.am_pm); //output: pm
    
    for(i=0;i<getData.to.length;i++)
    {
        alert(getData["to"][i].date); //output: 02/11/2013
        alert(getData["to"][i].hours); //output: 2
        alert(getData["to"][i].minutes); //output: 0
        alert(getData["to"][i].am_pm); //output: pm
    }
    data.field_74 = { 
          date: Data.date // start date
    	
      }
      
    data.field_200 =  { // start time
          hours: Data.hours, // start hours
    	  minutes: Data.minutes, // start minutes
          am_pm: Data.am_pm, // start hours
    				
      }
      
    data.field_201 = { // end date
          date: Data[to][i].date // start date
    				
      }
     
    data.field_134 = { // end time
          hours: Data[to][i].hours, // start hours
    	  minutes: Data[to][i].minutes, // start minutes
          am_pm: Data[to][i].am_pm, // start am_pm
    				
      }
        
      // update the record
      $.ajax({
        url: "https://api.knackhq.com/v1/objects/object_8/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!');
        }	
      });
        
    });

     

Please sign in to leave a comment.