I'm setting up a new request form where the user needs to pick the date they want the request to be done by. If they pick a date which is less than 2 weeks away, I'd like to alert them and also show them an extra question to answer.

I've used the code below to show and hide rows based on drop down field changes but can't figure out the how to get the value of the date field and and compare it to Today+14 days. Hope someone can help.

$("select[title$='Question here Required Field']").change(function() { if($(this).val()=='No') { alert('Message here.'); $('nobr:contains("The row I want to show/hide")').closest('tr').show(); } else { $('nobr:contains("The row I want to show/hide")').closest('tr').hide(); } });

(Sharepoint 2013 on-premises)

2 Answers 2


I'm assuming you are able to get the date value from the field, as you seem to be able to get other field values.

Comparing dates in JavaScript can be tricky. To be on the safe side I almost always use the MomentJS Library.This might be a good starting point: https://momentjs.com/docs/#/query/

If you want to use vanilla javascript, this is a good starting point: https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript. Then you need to cast the field value to a date object by

var fieldAsData = new Date(fieldValue)
  • Thank you for your prompt response - this was a great help as it introdcued me to the date object. I'm posting my final working code below with comments.
    – NCCadm
    Aug 22, 2018 at 17:11

This is the code that worked for me in the end.

//Trigger on focus to the date field
$("[title='Mailing date Required Field']").on("focus", function(event){
//Get the mailing date from the date field
var mailingDate = $(event.target).val();
//From Martijn's suggestion above I constructed a date object. However it returned the date in the wrong format. 
//From this page (https://stackoverflow.com/questions/3117262/javascript-date-objects-uk-dates) I got the solution (woggles' answer)
mailingDate = new Date(mailingDate.split('/')[2], mailingDate.split('/')[1] - 1, mailingDate.split('/')[0]); 

//Check to see if mailing date is a valid date - reason being, the code below was being triggered when someone clicked into the date field
    //get the date in 14 days time (thanks to AnthonyWJones here: https://stackoverflow.com/questions/563406/add-days-to-javascript-date)
    Date.prototype.addDays = function(days) {
        var lastMailingDate = new Date(this.valueOf());
        lastMailingDate.setDate(lastMailingDate.getDate() + days);
        return lastMailingDate;
    var lastMailingDate = new Date();
    lastMailingDate = lastMailingDate.addDays(13);

    //Compare the two dates
    if(mailingDate < lastMailingDate) {
        alert("Message here")
        $('H3:contains("The text in the H3 tag")').closest('tr').show();
        $('nobr:contains("The text in the nobr tag")').closest('tr').hide();
    } else {
        $('H3:contains("The text in the H3 tag")').closest('tr').hide();
        $('nobr:contains("The text in the nobr tag")').closest('tr').show();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.