简体   繁体   中英

How to check for the date and time in javascript asp.net c#?

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

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;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM