Wednesday, December 28, 2016

How to Compare Two Dates using JQuery.

How to Compare Two Dates using JQuery ?


function compareTwoDates(Date1, Date2) {
    var D1 = Date1.split(".");
    var bool = false;
    D1 = new Date(D1.pop(), D1.pop() - 1, D1.pop());
    var D2 = Date2.split(".");
    D2 = new Date(D2.pop(), D2.pop() - 1, D2.pop());
    if (D1 >= D2)
        bool = true;
    return bool;
}

How to get Current Date and Increment and Decrement date using JQuery

How to get Current Date using JQuery ?

 function getCurrentDate() {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!
    var yyyy = today.getFullYear();

    if (dd < 10) {
        dd = '0' + dd
    }

    if (mm < 10) {
        mm = '0' + mm
    }

    today = dd + '.' + mm + '.' + yyyy;
    return today;
}


Increment Date:
This function will give increment date by no. of days

Ex: current date 02.02.2017 increment by 2 days date will be 04.02.2017.

inputs are: incrementVal= 2 , and dateParam= 02.02.2017

function incrementDate(incrementVal, dateParam) {
    if (!isEmptyString(dateParam)) {
        var D1 = dateParam.split(".");
        dateParam = new Date(D1.pop(), D1.pop() - 1, D1.pop());
        dateParam.setDate(dateParam.getDate() + parseInt(incrementVal));

        var decrementedDate = ddmmyyyyFormat(dateParam);
        return decrementedDate;
    }

}

Result:
output:  04.02.2017.

Decrement Date:


This function will give decrement date by no. of days

Ex: current date 02.02.2017 decrement by 2 days date will be 31.01.2017.

inputs are: incrementVal= 2 , and dateParam= 02.02.2017

function decrementDate(decrementVal, dateParam) {
    if (!isEmptyString(dateParam)) {
        var D1 = dateParam.split(".");
        dateParam = new Date(D1.pop(), D1.pop() - 1, D1.pop());
        dateParam.setDate(dateParam.getDate() - parseInt(decrementVal));

        var decrementedDate = ddmmyyyyFormat(dateParam);
        return decrementedDate;
    }
}

Result:
output:  31.01.2017.

Format the Date: custom date format

This function will format the date as 02.02.2017.
inputs: date =current date.

function ddmmyyyyFormat(date) {
    var dateFromString = (('' + date.getDate()).length < 2 ? '0' : '') + date.getDate() + '.' +
        (('' + (date.getMonth() + 1)).length < 2 ? '0' : '') + (date.getMonth() + 1) + '.' +
         date.getFullYear();
    return dateFromString;
}

output:02.02.2017.