Setting field value rule for Date/Time field tyoe

Forums General discussion Setting field value rule for Date/Time field tyoe

Tagged: , , ,

This topic contains 7 replies, has 3 voices, and was last updated by  Maciek Grischke 1 week, 5 days ago.

  • Author
    Posts
  • #24752

    Scott Van Eerden
    Participant

    Hi Alexander,

    In DFFS, I have a rule that’s using the [today] value to populate a date/time column upon form load. I have been able to add days to that value (ex: [today]+3) to return the value 3 days from today, but I have not been able to figure out how to set the value with [today] plus 4 hours, for example. I’ve tried everything I can think of and have researched solution upon solution, but have been able to figure it out.

    Do you have any ideas on how I could set this up?

    Thank you

  • #24754

    Alexander Bautz
    Keymaster

    Hi,
    Unfortunately the Set field value function in the rule cannot set the time part so you must use some Custom JS. Just add this function to your Custom JS:

    function setDateColPlus4Hours(){
    	var dateVal = new Date();
    	dateVal.setHours(dateVal.getHours() + 4);
    	spjs.utility.setDateFieldFromDateObject("DateColumnName",dateVal,0);
    }

    Change DateColumnName to match your field name and insert the function name setDateColPlus4Hours in the Run these functions / trigger these rules field in your rule.

    Alexander

  • #25593

    Maciek Grischke
    Participant

    Hi Alexander,

    How can I use the above to set dates from an existing date field?

    I have 4 date fields and here’s what I want to do. Select shift start and end manually:

    Day:
    StartDate: 01/06/2019 7:30
    EndDate: 01/06/2019 22:00

    and then if option “night” is selected, fill in the following dates automatically:

    Night:
    NightStartDate 01/06/2019 22:00
    NightEndDate 02/06/2019 7:30 (next day)

  • #25598

    Alexander Bautz
    Keymaster

    Try it like this to set the dayshift:

    function setDayShift(){
    	var dateVal = new Date();
    	// Start
    	dateVal.setHours(7,30,0);
    	spjs.utility.setDateFieldFromDateObject("DateColumn1",dateVal,0);
    	// End
    	dateVal.setHours(22,0,0);
    	spjs.utility.setDateFieldFromDateObject("DateColumn2",dateVal,0);
    }
    setDayShift();

    and this to set the nightshift:

    function setNightShift(){
    	var dateVal = new Date();
    	// Start
    	dateVal.setHours(22,0,0);
    	spjs.utility.setDateFieldFromDateObject("DateColumn1",dateVal,0);
    	// End
    	dateVal.setHours(7,30,0);
    	dateVal.setDate(dateVal.getDate()+1);
    	spjs.utility.setDateFieldFromDateObject("DateColumn2",dateVal,0);
    }
    setNightShift();

    Change DateColumn1 and DateColumn2 to match your field names.

    Alexander

  • #25600

    Maciek Grischke
    Participant

    Hi Alexander,

    The day and night shifts are different for different people.

    7:30/22:00 was just an example.

    What I meant was, if the employee selects a ‘time in’ 9:00 and ‘time out’ 20:00 for a day shift, the same time (in this case 20:00-9:00) is copied to the night shift (if “night” option is set to “yes”) to make it easier for users.

    Please note, the second date for the night shift would be +1 day.

  • #25604

    Maciek Grischke
    Participant

    I got this working:

    //get Day End Date and Time
    function getEndDate() {
     var endDate = getFieldValue("EndDate");
     var endDate2 = endDate.split("/");
     var endDate3 = endDate2[1] + "/" + endDate2[0] + "/" +endDate2[2];
     var MyEndDate = endDate3.split(" ");
     var Time = MyEndDate[1];
     var MyEndDate2 = MyEndDate[0];
     var MyEndDate3 = MyEndDate2.split("/");
     var MyEndDate4 = MyEndDate3[2]+"-"+MyEndDate3[0]+"-"+MyEndDate3[1]+"T"+Time+"Z";
     var MyEndDate5 = new Date(MyEndDate4);
     MyEndDate5.setHours(MyEndDate5.getHours() - 1);
     spjs.utility.setDateFieldFromDateObject("SleepStart",MyEndDate5,0);
    // alert(MyDate5);
    }
    
    //get Day Start Date and Time
    function getStartDate() {
     var startDate = getFieldValue("StartDate");
     var startDate2 = startDate.split("/");
     var startDate3 = startDate2[1] + "/" + startDate2[0] + "/" +startDate2[2];
     var MyStartDate = startDate3.split(" ");
     var Time = MyStartDate[1];
     var MyStartDate2 = MyStartDate[0];
     var MyStartDate3 = MyStartDate2.split("/");
     var MyStartDate4 = MyStartDate3[2]+"-"+MyStartDate3[0]+"-"+MyStartDate3[1]+"T"+Time+"Z";
     var MyStartDate5 = new Date(MyStartDate4);
     MyStartDate5.setHours(MyStartDate5.getHours() - 1);
     spjs.utility.setDateFieldFromDateObject("SleepEnd",MyStartDate5,1);
    // alert(MyDate5);
    }
    

    I probably don’t need all of the date/time conversion.
    What do you think? 🙂

  • #25615

    Alexander Bautz
    Keymaster

    I don’t really understand what you are doing here, but you can use this to get the date value as a date object directly to avoid all the steps to get a proper date object:

    var startDate = spjs.utility.getDateFieldAsDateObject("EndDate");

    Alexander

  • #25619

    Maciek Grischke
    Participant

    I couldn’t get the date format “01/06/2019 11:00” to do what I wanted it to do hence all the extra steps.

    Thanks for the one line solution 😛

You must be logged in to reply to this topic.