1

here inputtext gives the value entered in the textbox and hidden field value returns current value.

My code till now:

if (inputText.value.length != 0) {
    if (inputText.value < document.getElementById('<%=HdnDate.ClientID%>').value) {
        alert("Please ensure that the Date is greater than or equal to the Current Date.");
        inputText.value = "";
        return false;
    }
}
khakiout
  • 2,372
  • 25
  • 32
sona
  • 1,552
  • 3
  • 18
  • 37

4 Answers4

1

Assuming that the input date is in a format like d/m/y, then you can convert that to a date object using:

function parseDate(s) {
  var b = s.split(/\D+/g);
  return new Date(b[2], --b[1], b[0]);
}

which creates a date object for 00:00:00 on the specified date.

To compare to the current date, create a new Date object and set the time to 00:00:00.0:

var today = new Date();
today.setHours(0,0,0,0);

Then convert the string to a Date and compare the two:

var otherDay = parseDate('21/4/2013');

console.log(otherDay + ' less than ' + today + '?' + (otherDay < today)); // ... true

Edit

It seems your date format is 4-may-2014. In that case:

function parseDate(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:12};
  var b = s.split(/-/g);
  return new Date(b[2], months[b[1].substr(0,3).toLowerCase()], b[0]);
}
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Try this code :

var d1= new Date(); // get the current date

var enddate = inputText.value; // text box value

var d2 = enddate.split('/');

d2 = new Date(d2.pop(), d2.pop() - 1, d2.pop());


if (d2 >= d1)
 {
    // do something
 }
Kumar Manish
  • 3,746
  • 3
  • 37
  • 42
  • Assuming that *d1* is actually *startdate*, note that *startdate* will use the current time whereas *d2* will not and be set to 00:00:00. So even if *d2* and *startdate* are the same date, `d2 >= d1` will almost certainly always be false unless *startdate* is created at exactly midnight (to the millisecond 00:00:00.000) at the start of the date. – RobG May 09 '14 at 07:17
0

Try This

var date1=inputText.value;
var date2=document.getElementById('<%=HdnDate.ClientID%>').value;
var record1 = new Date(date1);
var record2 = new Date(date2);
if(record1 <= record2)
{
      alert("Please ensure that the Date is greater than or equal to the Current Date.");

}
joker
  • 982
  • 9
  • 23
  • But how it will check for month and year?@joker – sona May 09 '14 at 07:08
  • it will check for whole date not just for day, i just use variable name record_day1 – joker May 09 '14 at 07:09
  • Thanks for the reply.let me try.@joker – sona May 09 '14 at 07:10
  • For alert message record1 and record2 it is showing invalid date.@joker – sona May 09 '14 at 07:20
  • It does not make any sense at all to get the parts of a date, then construct a non–standard string that is then parsed by *Date.parse*. Just use the parts directly in the Date constructor. E.g. assuming a standard d/m/y string: `record1 = new Date(record_day1[2], --record_day1[1], record_day1[0])`. – RobG May 09 '14 at 07:22
  • change my answer try this, may be it helps you. . .@sona – joker May 09 '14 at 07:28
0

Try This:

<script type="text/javascript">
    var dateObj = new Date();
    var month = dateObj.getUTCMonth();
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();
    var dateSplitArray = "";
    var enddate = '05/05/2014'; // set your date here from txtbox
    var IsValidDate = false;
    splitString(enddate, "/");
    if (year >= dateSplitArray[2]) {
        if (month >= dateSplitArray[1]) {
            if (day >= dateSplitArray[0]) {
                IsValidDate = true;
            }
        }
    }
    if (IsValidDate == false) {
        alert("Please ensure that the Date is greater than or equal to the Current Date.");
    }
    else {
        alert("Please proceed, no issue with date");
    }
    function splitString(stringToSplit, separator) {
        dateSplitArray = stringToSplit.split(separator);
    }
</script>
NayeemKhan
  • 1,210
  • 7
  • 19
  • 38
  • my date format is ex:4-may-2014. how to check for mothng?in the above code@nayeemkhan – sona May 09 '14 at 09:16
  • But only one thing we can add when month is eqau and when month is greater condition. if (dateSplitArray[2] >= year) { if (monthNumber = month) { if (dateSplitArray[0] >= day) { IsValidDate = true; } } else if (monthNumber >month) { IsValidDate = true; }@nayeemkhan – sona May 09 '14 at 10:59
  • Why use UTC values? If a user at say UTC-0500 runs the above at 21:00 on 30 June, the above will return UTC values for 1 July. Also, month numbers in ECMAScript are zero based, this answer doesn't allow for that (e.g. `date.getMonth()` returns 5 for June). – RobG May 09 '14 at 14:34
  • @sona in case of 4-may-2014, u may need to maintain an array of month name to compare months, thanks. – NayeemKhan May 12 '14 at 06:33
  • @RobG It was my choice to go for UTC as date retrieval is simple. you can convert or use any other format too, no restriction :). For month please add 1. – NayeemKhan May 12 '14 at 06:38