1

I have a text box using jquery date and time iam choosing a date and time so this will be in my text box 2014/06/11 18:50 On submit button i am calling a javascript

 <asp:Button ID="bt_schedule" runat="server" CssClass="button" Text="Submit" OnClientClick="return btnSubmit_Click();" 
                            OnClick="bt_schedule_Click" />

my requirement is date should be greater than and equal to today's date and time should be greater than 5 minute to current time. so i am using something like below

<script type="text/javascript">
            function btnSubmit_Click() {

                var strval = document.getElementById("<%= tb_calimage.ClientID %>").value;
                var dateParts = strval.split(" ");
                var date = dateParts[0];
                var time = dateParts[1];
                var datecompare = new Date(date);
                var currentDate = new Date();

 var sysdate = currentDate.getHours();
                var systime = currentDate.getMinutes();
                if (datecompare >= currentDate ) {
                    alert('Date greater than or equal t0 Today');
                    return false;
                } 

                return true;
            }

and also currentDate is showing date with time so i am not able to get it properly.. and also how to check with time. Please help

1 Answer 1

1

Basically, your current format of string is good to go.

So, if in Javascript you write:

var requestedDate = new Date("2014/06/11 18:50");

var oldDate = new Date("2014/06/11 18:45");

and try to do a subtraction.

var val = requestedDate-oldDate;

val would be: 300000.

Extending this further you could just use var oldDate = new Date()

and check if val>300000, then you have a time greater than 5 minutes from now :)

Complete code:

function btnSubmit_Click() {

                var strval = document.getElementById("<%= tb_calimage.ClientID %>").value;
                var datecompare = new Date(strval);
                var currentDate = new Date();
                if (datecompare-currentDate <300000 ) {
                    alert('Please select date-time more than 5 minutes from now.');
                    return false;
                } 

                return true;
            }
Sign up to request clarification or add additional context in comments.

Comments

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.