Formatting Date

Below code is primarily used prior to comparing two dates. Especially when one date is retrieved using webapi and other from standard getAttribute()

AP.Common.GetFormatedDate = function (stringDate, format)
{
    // Format date
    var f = new Date(stringDate);
    //var d = f.getDate();
    //("0" + this.getDate()).slice(-2)
    var d = ("0" + f.getDate()).slice(-2);
    // var m = f.getMonth() + 1;
    var m = ("0" + (f.getMonth() + 1)).slice(-2)
    var y = f.getFullYear();
      
    if (format === "US")
    {
        var cleanDate = m + "/" + d + "/" + y;
    }
    else
    {
        var cleanDate = d + "/" + m + "/" + y;
    }
    return cleanDate;
}

Now when you get the date from WebAPI or getAttribute, send it to this function to get back uniform date format.

let formatedDate = AP.Common.GetFormatedDate(retrievedDate);

This will return date in UK format (For example, 27/10/2022).

Before using this dates in compare, you must convert them to date by calling Date.parse

To do this, simply pass “US” parameter when calling the function

let formatedDate = AP.Common.GetFormatedDate(retrievedDate, "US"); // Extra parameter

Once converted to US date format Date.parse will work

if (Date.parse(formatedDate1) > Date.parse(formatedDate2))
        {
            // set notification on the form or field
        }

Note : Date.parse requires “mm/DD/YYYY” formate so “27/10/2022” will return NaN and hence passing “US” parameter so the return date is in US formate 10/27/2022.