PeterV

Check if a date is in the paste - region locale

Discussion created by PeterV on May 16, 2017
Latest reply on May 16, 2017 by Matthias_Walter

Have you ever wanted to check that a date is in the past?

 

There are 2 things you need to do

  • Get the current site's regional locale from the SharePoint context object
  • Check the type of object that is returned for your field

 

If the field is a Date object then your locale is probably en-US.

For me being in Australia the field value is returned as a string    (en-AU)

 

Here's my code, I've been overly cautious and left redundant code just in case

 

**********************************************************************

// Handles type or picked dates

var result = true;

var day, month, year;

var duedate;

var today = new Date();

 

function getNewDate(day, month, year){  

    var date = new Date(year, month-1, day, 0, 0, 0, 0);     

    return date;

}

 

if ([[Deadline]] === "") { return false; }

 

alert('Site regional setting: ' + _spPageContextInfo.currentCultureName);

 

// This will pause execution ONLY if the developer tools window is open

// Use this when 'Testing' skybow forms in edit mode

//debugger;

 

// It appears that the date field is just a string and not a Date object for locales other than USA

if ([[Deadline]].getMonth == null) {

   

    alert('Deadline is a string');

   

    if (_spPageContextInfo.currentCultureName == "en-AU") {

        day = [[Deadline]].split("/")[0];

        month = [[Deadline]].split("/")[1];    

        year = [[Deadline]].split("/")[2];     

        duedate = getNewDate(day, month, year);

    }

    else {   

        day = [[Deadline]].split("/")[1];

        month = [[Deadline]].split("/")[0];    

        year = [[Deadline]].split("/")[2];     

        duedate = getNewDate(day, month, year);

    }

}

else {

   

    alert('Deadline is a Date object');

   

    if (_spPageContextInfo.currentCultureName == "en-AU") {  

        day = [[Deadline]].getMonth()+1;

        month = [[Deadline]].getDate(); 

        year = [[Deadline]].getFullYear();     

        duedate = getNewDate(day, month, year);

    }else {  

        day = [[Deadline]].getDate();   

        month = [[Deadline]].getMonth()+1;     

        year = [[Deadline]].getFullYear();     

        duedate = getNewDate(day, month, year);

    }

}

 

alert('Today: ' + today + '\n Deadline: ' + duedate);

 

if (duedate < today){     

    result = false;

}

 

return result;

**********************************************************************

Outcomes