It seems like your intention is to compare the formatted strings, instead of moment
or Date
objects. If you are using moment.js, it's recommended to compare moment
objects using the functions isBefore
or isAfter
, rather than comparison operators.
Don't forget to provide an input format string during parsing to avoid falling back to the Date
constructor and receiving a deprecation warning in your console.
In the example below, I set the variable fmt
as the input format and output format for dates. Keep in mind that the RFC822 format used here is not ideal; consider using the ISO8601 format instead.
Additionally, note that I use the moment.utc
function to explicitly interpret the input in UTC time, rather than relying on the "GMT" abbreviation, which is crucial.
$(function() {
var fmt = "ddd, D MMM YYYY HH:mm:ss [GMT]";
start = moment.utc("Thu, 29 Oct 2015 18:00:00 GMT", fmt);
end = moment.utc("Fri, 30 Oct 2015 00:00:00 GMT", fmt);
$('#divLocal').html(start.format(fmt));
$('#divLocal2').html(end.format(fmt));
if (end.isAfter(start)) {
alert("work");
} else {
alert("not work");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
<div id="divLocal"></div>
<div id='divLocal2'></div>
(Updated jsFiddle)