Friday, September 12, 2014

Get a date without time in JavaScript

Very often we need to compare two dates in javascript

So for example you need to get today’s date to compare it with any other date you will use the following var todayDate = new Date();

But again with this you will also get the time in this, if however you do not need to time part use the following function
var todayDate = new Date(new Date().setHours(0, 0, 0, 0));

So to do a date compare your function will look something similar to the function below


function comapreDates(context) {
    var date1  = Xrm.Page.ui.controls.get("new_date1").getAttribute().getValue();
    var todayDate = new Date(new Date().setHours(0, 0, 0, 0));
    if (date1  != null) {
        if (date1  < todayDate) {
            alert("date1 cannot be less than today");
            context.getEventArgs().preventDefault();
            return false;
        }
    }

}