1

I am using Moment js to get my proper date format.

SO what I do it, I have a textbox where I am inserting todays date like 09/02/2016 it is showing properly here

var dt1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value).zone(0).format("dd/MM/yyyy HH:mm");

but when I check the same date in the alert like below

alert(dt1);

I get value as Mon Feb 01 2016 09:00:00 GMT +0530

I tried the logic from here but wasn't helpful to me. any idea of what's wrong.

MY HTML

<asp:TextBox ID="TextBox1" runat="server" Width="80" ></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>

NOTE: I want date in dd/MM/yyyy format

Also I took the js from below link:-

http://momentjs.com/downloads/moment.min.js

UPDATE

function ValidateRecord() {
        var StrPriError = "";
        var dt1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value, "dd/MM/yyyy HH:mm");
        var dt2 = moment(document.getElementById('TextBox2').value + " " + document.getElementById('DrpTime3').value, "dd/MM/yyyy HH:mm");
        var dt3 = moment(document.getElementById('TextBox3').value + " " + document.getElementById('DrpTime4').value, "dd/MM/yyyy HH:mm");
        var dt4 = moment(document.getElementById('TextBox4').value + " " + document.getElementById('DrpTime5').value, "dd/MM/yyyy HH:mm");
        alert(dt1);

        if (StrPriError != "") {
            alert(StrPriError);
            return false;
        }
        else {
            return true;
        }
    }
Community
  • 1
  • 1
Nad
  • 4,605
  • 11
  • 71
  • 160
  • The format you're mentioning is actually default JavaScript format for dates. Can you post some more JavaScript code because this should work fine. – martin Feb 09 '16 at 07:46
  • @Martin: updated the question, other than that, I have added the moment js in my page – Nad Feb 09 '16 at 07:50
  • 1
    @Martin—no, it's not. The only format specified in [*ECMA-262*](http://ecma-international.org/ecma-262/6.0/index.html#sec-date-time-string-format) is ISO 8601, and 09/02/2016 is not conforming, nor is Mon Feb 01 2016 09:00:00 GMT +0530 (which is different to what my browser returns for *date.toString*). – RobG Feb 09 '16 at 08:47
  • @RobG: agreed with u Rob, because if it was same, I would have cracked it without using `moment.js` only. I just added `moment.js` to get the required format – Nad Feb 09 '16 at 08:49
  • @coder—it's best to manually parse strings, a library (like moment.js) can help with that. ;-) – RobG Feb 09 '16 at 08:50
  • @RobG: yeah that's what I _tried_ but was still not getting the required format, so posted this question and got the necessary help from **Enver Dzhaparoff** ;) – Nad Feb 09 '16 at 08:52

1 Answers1

1

Try to set second argument in moment parser method to actual parsing format

var dt1 = moment(value1 + " " + value2, "DD/MM/YYYY HH:mm");

UPD:

To check equality of two dates use this code:

 date_1 = moment(value1 + " " + value2, "DD/MM/YYYY HH:mm");
 date_2 = moment(value3 + " " + value4, "DD/MM/YYYY HH:mm");

 date_1.isSame(date_2) // return boolean value

UPD2:

MomentJS has some methods to check equality of dates.

 date_1.isAfter(date_2)              // date_1 > date_2
 date_1.isBefore(date_2)             // date_1 < date_2
 date_1.isBetween(date_2, date_3)    // date_2 < date_1 < date_3
 date_1.isSame(date_2)               // date_1 === date_2
 date_1.isSameOrAfter(date_2)        // date_1 >= date_2
 date_1.isSameOrBefore(date_2)       // date_1 <= date_2
Enver Dzhaparoff
  • 4,341
  • 3
  • 19
  • 21
  • not working, getting date as `We/02/yyyy 03:30` I tried like this `var dt1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value, "DD/MM/YYYY HH:mm").zone(0).format("dd/MM/yyyy HH:mm"); alert(dt1); return false;` – Nad Feb 09 '16 at 07:57
  • not required, i think your edited code is giving me the expected output – Nad Feb 09 '16 at 07:58
  • just one thing while inserting two equal dates in 2 textbox, the condition is not matching to true like this `if (date1 == date2 {alert("dates are same"}` – Nad Feb 09 '16 at 08:10
  • my date1 and date 2 are `var date1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value, "DD/MM/YYYY HH:mm"); var date2 = moment(document.getElementById('TextBox2').value + " " + document.getElementById('DrpTime3').value, "DD/MM/YYYY HH:mm");` – Nad Feb 09 '16 at 08:11
  • i don't want like that, i want to compare `if (date1 === date2 || date1 === date3 || date1 === date4 || date2 === date3 || date2 === date4 || date3 === date4) { alert("Dates and time cannot be same"); }` if all dates and time are same it should alert a message as displayed – Nad Feb 09 '16 at 08:40
  • `if( date_1.isSame(date_2) || date_1.isSame(date_3) || date_1.isSame(date_4) || date_2.isSame(date_3) || date_2.isSame(date_4) || date_3.isSame(date_4)) { alert("Dates and time cannot be same"); }` - so what's the problem? – Enver Dzhaparoff Feb 09 '16 at 08:44
  • aahhh, got it. so it would be same for cheking less than or greater than ?? for ex:- `if(date2 < date1) {alert('date 2 cannot be less than date1}` – Nad Feb 09 '16 at 08:46
  • it would be same if parts of date is same (day === day, month === month, year === year, hour === hour, minutes === minutes). If one date is less or greater than other `isSame` method will be return `false`. – Enver Dzhaparoff Feb 09 '16 at 08:49
  • ok. will this work `if (date2 < date1) { alert("date 2 is less"); }` ??? just for a cross check – Nad Feb 09 '16 at 08:52
  • Superb, that's what I wanted :) again a lot more thanks ;) – Nad Feb 09 '16 at 08:56