0

script is ---

 function TimeSpentForFutureDate() {
      var toDate = new Date();
      toDate.setMinutes(0);
      toDate.setSeconds(0);
      toDate.setHours(0);
      toDate.setMilliseconds(0);
  //Here after selecting future date also, this condition is failing.The textbox
      // containing a future date

      if (document.getElementById('<%= txtDate.ClientID%>').value > toDate) {
          var timespent = jPrompt('Enter Time Spent:', '', 'Enter Time Spent', 
          function (r) {
              if (r) {
                 document.getElementById('<%= 
                    hiddenFieldFutureDateSelectTimeSpent.ClientID%>').value = r;
                 jAlert('You entered ' + r);
              }
              else {
                 var todaysDate = new Date();
                 jAlert('You had not entered the Time Spent', 'Message');
              }
              });
            }
            else {
                document.getElementById('<%=
                    hiddenFieldFutureDateSelectTimeSpent.ClientID%>').value = 
                    timespent;
                document.getElementById('<%= txtDate.ClientID%>').value = toDate;
            }
        }

In the above code I'm checking that if text box 'txtDate' will contain a future date.

[ i.e date greater then today's date it will prompt for entering time spent and then store that time spent into an hidden field.]

I'm not able to convert a string into date time object for comparison. Please help me to resolve this issue.

Thanks in advance.

3
  • What is that mysterious #txtDate string? Commented Jun 6, 2012 at 9:42
  • This is a textBox containing date selected from an calendar extender. Commented Jun 6, 2012 at 9:46
  • 1
    in which format calendar extender get date Commented Jun 6, 2012 at 9:51

1 Answer 1

2

Date.parse will do the trick

var dateToCovert=document.getElementById('<%= txtDate.ClientID%>').value;
var sDate = new Date(Date.parse(dateToCovert,"MM/dd/yyyy"));

the format("MM/dd/yyyy") of course can be changed against your needs

note:

make sure dateToCovert is not null and it is a valid format

Date.parse doesn't work in IE7/8.

Sign up to request clarification or add additional context in comments.

3 Comments

How to put that parse in my code i.e. if (document.getElementById('<%= txtDate.ClientID%>').value > toDate), Any suggestion?
You should advise against the compatibility, though. Date.parse doesn't work in IE7/8.
And here is a shim for Date.parse in case you need the compatibility :) gist.github.com/303249

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.