Validate Datetime with current Datetime using jQuery


Thursday, January 27, 2011

Suppose you want to validate both Date i.e with time and without time with current date and time,
that is, selected data should be greater that current date.

with time
27/01/2011 13:01

without time
27/01/2011

Below is the fuction, you can make use of it and you can check that selected date and time is greater or lesser than current date and time.

This function will return true and false,

if selected date is less than current date and time, then it will return false, else it will return true.




function CompareToForToday(value,params)
            {
               if(value!='')
        {
                    var mdy = value.split('/')     //Date and month split
                    var mdyhr= mdy[2].split(' ');  //Year and time split
                   
                    
                    if(mdyhr[1] == undefined){
                        var valuedate= new Date(mdyhr[0], mdy[1]-1, mdy[0]);
                    }
            else
                    {
                        var mdyhrtime=mdyhr[1].split(':');
                        var valuedate= new Date(mdyhr[0], mdy[1]-1, mdy[0], mdyhrtime[0], mdyhrtime[1]);
                    }

                    var d = new Date();
                    if(mdyhr[1] == undefined){
                        var todaydate = new Date(d.getFullYear(), d.getMonth(), d.getDate());
                    }
                    else
                    {
                        var mdyhrtime=mdyhr[1].split(':');
                        var todaydate = new Date(d.getFullYear(), d.getMonth(), d.getDate(),d.getHours(),d.getMinutes());
                    }

                    return Date.parse(valuedate) > Date.parse(todaydate);
                }
                else
                {
                    return true;
                }
            }



Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Senior Asp.Net Developer
Email: raj143svmit@gmail.com
e-Procurement Technologies Ltd (India)
www.abcprocure.com