0

I want to validation in javascript by comparing DATES and TIME.

I want in the dd/MM/yyyy format but I dont know which format it is taking. I debugged the js code and got the format. Below is the screenshot which is giving me any other date while I am inserting TODAYS date.

Image1

and below is the code:-

function ValidateRecord() {
        var StrPriError = "";



        if (document.getElementById('TextBox1').value == "" || document.getElementById('TextBox2').value == "" || document.getElementById('TextBox3').value == "" || document.getElementById('TextBox4').value == "") {
            StrPriError += "Dates cannot be blank \n";
        }
        else {
            var dt1 = new Date(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value);
            var dt2 = new Date(document.getElementById('TextBox2').value + " " + document.getElementById('DrpTime3').value);
            var dt3 = new Date(document.getElementById('TextBox3').value + " " + document.getElementById('DrpTime4').value);
            var dt4 = new Date(document.getElementById('TextBox4').value + " " + document.getElementById('DrpTime5').value);

            if (dt1.getTime() == dt2.getTime() || dt1.getTime() == dt3.getTime() || dt1.getTime() == dt4.getTime() || dt2.getTime() == dt3.getTime() || dt2.getTime() == dt4.getTime() || dt3.getTime() == dt4.getTime()) {
                StrPriError += "Dates and Times cannot be same \n";
            }
            else {
               // StrPriError += "Good to go \n";
            }

            if (dt2.getTime() < dt1.getTime()) {
                StrPriError += "Reminder 2 cannot be less than reminder 1 \n";
            }

            if (dt3.getTime() < dt1.getTime()) {
                StrPriError += "Reminder 3 cannot be less than reminder 1 \n";
            }

            if (dt4.getTime() < dt1.getTime()) {
                StrPriError += "Reminder 4 cannot be less than reminder 1 \n";
            }
            if (dt3.getTime() < dt2.getTime()) {
                StrPriError += "Reminder 3 cannot be less than reminder 2 \n";
            }

            if(dt4.getTime() < dt2.getTime()) {
                StrPriError += "Reminder 4 cannot be less than reminder 2 \n";
            }

            if (dt4.getTime() < dt3.getTime()) {
                StrPriError += "Reminder 4 cannot be less than reminder 3 \n";
            }
        }
        if (StrPriError != "") {
            alert(StrPriError);
            return false;
        }
        else {
            return true;
        }
    }

Also see the HTML:-

<td colspan="1px" style="width: 13%" class="field">
                <asp:TextBox ID="TextBox1" runat="server" Width="80" Enabled="false"></asp:TextBox>
                <cc3:Calendar ID="Calendar1" runat="server" DatePickerMode="true" TextBoxId="TextBox1"
                    DatePickerImagePath="../../Images/icon2.gif" CultureName="en-GB">
                </cc3:Calendar>
                <asp:DropDownList ID="DrpTime" runat="server" Width="65px">
                    <asp:ListItem Value="09:00">09:00</asp:ListItem>
                    <asp:ListItem Value="13:00">13:00</asp:ListItem>
                    <asp:ListItem Value="17:00">17:00</asp:ListItem>
                    <asp:ListItem Value="21:00">21:00</asp:ListItem>
                </asp:DropDownList>
            </td>
            <td colspan="1px" style="width: 13%" class="field">
                <asp:TextBox ID="TextBox2" runat="server" Width="80" Enabled="false"></asp:TextBox>
                <cc3:Calendar ID="Calendar2" runat="server" DatePickerMode="true" TextBoxId="TextBox2"
                    DatePickerImagePath="../../Images/icon2.gif" CultureName="en-GB">
                </cc3:Calendar>
                <asp:DropDownList ID="DrpTime3" runat="server" Width="65px">
                    <asp:ListItem Value="09:00">09:00</asp:ListItem>
                    <asp:ListItem Value="13:00">13:00</asp:ListItem>
                    <asp:ListItem Value="17:00">17:00</asp:ListItem>
                    <asp:ListItem Value="21:00">21:00</asp:ListItem>
                </asp:DropDownList>
            </td>

Because of this I am not able to do validation properly. Any suggestion how to achieve this

2 Answers 2

2

You can easily achieve this by using moment.js ,

var dt1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value, "DD/MM/yyyy HH:mm");
Sign up to request clarification or add additional context in comments.

9 Comments

do I need to install that moment.js plugin ??
You have to include moment.js script in your page
can you give me the link for the same, as most of the sites are block on my end.
i tried like that by inserting today's date but I am getting value as Mon Feb 01 2016 09:00:00 GMT +0530 Time and Year are correct, just date is taking one week back. WHY ?
What are the values of document.getElementById('TextBox1').value? and document.getElementById('DrpTime').value?
|
2
  var monthNames = [
                       "January", "February", "March",
                       "April", "May", "June", "July",
                       "August", "September", "October",
                       "November", "December"
                       ];

                var dd =  new Date(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value);

                output.value = dd.getDate() + ' ' + monthNames[dd.getMonth()] + ' ' + dd.getFullYear() + ' ' + dd.getHours() + ':' + dd.getMinutes() + ':' + dd.getSeconds();

I think the above code will helps you.

8 Comments

let me try and check
getting error as Microsoft JScript runtime error: 'output' is undefined
what is the actual string value coming out of the date textbox
@coder I just mentioned output.value for you reference. You just edit the code and assign the output where you want.
I want to check first whether it is taking proper format or not. i will update the question for your reference.just show me how to do that
|

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.