I believe you are looking for the query functions, isBefore, isSame, and isAfter.
But it's a bit difficult to tell exactly what you're attempting. Perhaps you are just looking to get the difference between the input time and the current time? If so, consider the difference function, diff. For example:
moment().diff(date_time, 'minutes')
A few other things:
There's an error in the first line:
var date_time = 2013-03-24 + 'T' + 10:15:20:12 + 'Z'That's not going to work. I think you meant:
var date_time = '2013-03-24' + 'T' + '10:15:20:12' + 'Z';Of course, you might as well:
var date_time = '2013-03-24T10:15:20:12Z';You're using:
.tz('UTC')incorrectly..tzbelongs to moment-timezone. You don't need to use that unless you're working with other time zones, likeAmerica/Los_Angeles.If you want to parse a value as UTC, then use:
moment.utc(theStringToParse)Or, if you want to parse a local value and convert it to UTC, then use:
moment(theStringToParse).utc()Or perhaps you don't need it at all. Just because the input value is in UTC, doesn't mean you have to work in UTC throughout your function.
You seem to be getting the "now" instance by
moment(new Date()). You can instead just usemoment().
Updated
Based on your edit, I think you can just do this:
var date_time = req.body.date + 'T' + req.body.time + 'Z';
var isafter = moment(date_time).isAfter('2014-03-24T01:14:00Z');
Or, if you would like to ensure that your fields are validated to be in the correct format:
var m = moment.utc(req.body.date + ' ' + req.body.time, "YYYY-MM-DD HH:mm:ss");
var isvalid = m.isValid();
var isafter = m.isAfter('2014-03-24T01:14:00Z');
Answer from Matt Johnson-Pint on Stack OverflowI believe you are looking for the query functions, isBefore, isSame, and isAfter.
But it's a bit difficult to tell exactly what you're attempting. Perhaps you are just looking to get the difference between the input time and the current time? If so, consider the difference function, diff. For example:
moment().diff(date_time, 'minutes')
A few other things:
There's an error in the first line:
var date_time = 2013-03-24 + 'T' + 10:15:20:12 + 'Z'That's not going to work. I think you meant:
var date_time = '2013-03-24' + 'T' + '10:15:20:12' + 'Z';Of course, you might as well:
var date_time = '2013-03-24T10:15:20:12Z';You're using:
.tz('UTC')incorrectly..tzbelongs to moment-timezone. You don't need to use that unless you're working with other time zones, likeAmerica/Los_Angeles.If you want to parse a value as UTC, then use:
moment.utc(theStringToParse)Or, if you want to parse a local value and convert it to UTC, then use:
moment(theStringToParse).utc()Or perhaps you don't need it at all. Just because the input value is in UTC, doesn't mean you have to work in UTC throughout your function.
You seem to be getting the "now" instance by
moment(new Date()). You can instead just usemoment().
Updated
Based on your edit, I think you can just do this:
var date_time = req.body.date + 'T' + req.body.time + 'Z';
var isafter = moment(date_time).isAfter('2014-03-24T01:14:00Z');
Or, if you would like to ensure that your fields are validated to be in the correct format:
var m = moment.utc(req.body.date + ' ' + req.body.time, "YYYY-MM-DD HH:mm:ss");
var isvalid = m.isValid();
var isafter = m.isAfter('2014-03-24T01:14:00Z');
You should be able to compare them directly.
var date = moment("2013-03-24")
var now = moment();
if (now > date) {
// date is past
} else {
// date is future
}
$(document).ready(function() {
$('.compare').click(function(e) {
var date = $('#date').val();
var now = moment();
var then = moment(date);
if (now > then) {
$('.result').text('Date is past');
} else {
$('.result').text('Date is future');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" name="date" id="date" value="2014-12-18" placeholder="yyyy-mm-dd">
<button class="compare">Compare date to current date</button>
<br>
<div class="result"></div>
The docs are pretty clear that you pass in a second parameter to specify granularity.
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
moment('2010-10-20').isAfter('2010-01-01', 'year'); // false moment('2010-10-20').isAfter('2009-12-31', 'year'); // trueAs the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
For your case you would pass 'day' as the second parameter.
Meanwhile you can use the isSameOrAfter method:
moment('2010-10-20').isSameOrAfter('2010-10-20', 'day');
Docs: https://momentjs.com/docs/#/query/is-same-or-after/