How to compare only date in moment.js - javascript

I am new to moment.js. I have a date object and it has some time associated with it. I just want to check if that date is greater than or equal to today's date, excluding the time when comparing.
var dateToCompare = 2015-04-06T18:30:00.000Z
I just want to check if dateToCompare is equal or greater than today's date.
I have checked isSame of moment.js, but it seems to take string and only the date part. But I do not want to convert my date to string or manipulate it further. Because I am worried that javascript may do something unexpected when converting that date to string(like adding the offset or dst,etc), or may be I am wrong.
Sample isSame() from docs
moment('2010-10-20').isSame('2010-10-20');
Also I am looking for something like isSame() and isAfter() combined as one statement.
I need to compare using moment.js only.Please do not suggest plain javascript date comparison.

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'); // true
As 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/

In my case i did following code for compare 2 dates may it will help you ...
var date1 = "2010-10-20";
var date2 = "2010-10-20";
var time1 = moment(date1).format('YYYY-MM-DD');
var time2 = moment(date2).format('YYYY-MM-DD');
if(time2 > time1){
console.log('date2 is Greter than date1');
}else if(time2 > time1){
console.log('date2 is Less than date1');
}else{
console.log('Both date are same');
}
<script src="https://momentjs.com/downloads/moment.js"></script>

You could use startOf('day') method to compare just the date
Example :
var dateToCompare = moment("06/04/2015 18:30:00");
var today = moment(new Date());
dateToCompare.startOf('day').isSame(today.startOf('day'));

moment('2022-01-07').isAfter(moment().format('YYYY-MM-DD'), 'day');
checking
2022-01-07 this with current date
if current date is beyond 6th Jan 2022, it returns false!

For checking one date is after another by using isAfter() method.
moment('2020-01-20').isAfter('2020-01-21'); // false
moment('2020-01-20').isAfter('2020-01-19'); // true
For checking one date is before another by using isBefore() method.
moment('2020-01-20').isBefore('2020-01-21'); // true
moment('2020-01-20').isBefore('2020-01-19'); // false
For checking one date is same as another by using isSame() method.
moment('2020-01-20').isSame('2020-01-21'); // false
moment('2020-01-20').isSame('2020-01-20'); // true

moment('date').toDate().getTime() > moment()

Related

Why does my momentjs issameorafter() not functioning properly [duplicate]

I am new to moment.js. I have a date object and it has some time associated with it. I just want to check if that date is greater than or equal to today's date, excluding the time when comparing.
var dateToCompare = 2015-04-06T18:30:00.000Z
I just want to check if dateToCompare is equal or greater than today's date.
I have checked isSame of moment.js, but it seems to take string and only the date part. But I do not want to convert my date to string or manipulate it further. Because I am worried that javascript may do something unexpected when converting that date to string(like adding the offset or dst,etc), or may be I am wrong.
Sample isSame() from docs
moment('2010-10-20').isSame('2010-10-20');
Also I am looking for something like isSame() and isAfter() combined as one statement.
I need to compare using moment.js only.Please do not suggest plain javascript date comparison.
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'); // true
As 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/
In my case i did following code for compare 2 dates may it will help you ...
var date1 = "2010-10-20";
var date2 = "2010-10-20";
var time1 = moment(date1).format('YYYY-MM-DD');
var time2 = moment(date2).format('YYYY-MM-DD');
if(time2 > time1){
console.log('date2 is Greter than date1');
}else if(time2 > time1){
console.log('date2 is Less than date1');
}else{
console.log('Both date are same');
}
<script src="https://momentjs.com/downloads/moment.js"></script>
You could use startOf('day') method to compare just the date
Example :
var dateToCompare = moment("06/04/2015 18:30:00");
var today = moment(new Date());
dateToCompare.startOf('day').isSame(today.startOf('day'));
moment('2022-01-07').isAfter(moment().format('YYYY-MM-DD'), 'day');
checking
2022-01-07 this with current date
if current date is beyond 6th Jan 2022, it returns false!
For checking one date is after another by using isAfter() method.
moment('2020-01-20').isAfter('2020-01-21'); // false
moment('2020-01-20').isAfter('2020-01-19'); // true
For checking one date is before another by using isBefore() method.
moment('2020-01-20').isBefore('2020-01-21'); // true
moment('2020-01-20').isBefore('2020-01-19'); // false
For checking one date is same as another by using isSame() method.
moment('2020-01-20').isSame('2020-01-21'); // false
moment('2020-01-20').isSame('2020-01-20'); // true
moment('date').toDate().getTime() > moment()

Compare if 2 dates are the same in javascript

var beginningTime = moment('1635750314812', 'YYYY/MM/DD'); // Today date
var endTime = moment(undefined, 'YYYY/MM/DD') // Today date
console.log(beginningTime.isSame(endTime)); //expect true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I am using moment js to get if 2 date are the same using the above pattern. But now i get false instead of true. How to solve the issue?
Instead of formatting the beginningTime and endTime separately, you should take a look at the 'granularity' argument for isSame().
I made a few adjustments to your code for it to work, and also look cleaner.
var beginningTime = moment(1635750314812); // Today date (from timestamp)
var endTime = moment() // Today date
console.log(beginningTime.isSame(endTime, 'day')); //expect true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
It first simply creates a moment of your timestamp and a moment of right now, whenever now is.
Then it will compare the two moments with a granularity of a day, effectively checking if the day and everything bigger (month, year) is the same.

Moment.js comparing to times range with am pm

I have an dates array which has an array of appointment times in each date. I loop through a given date to get the appointment times like this :
var newdate = moment(appdate).format('DD/MM/YYYY'); //eg newdate is 04/04/2016
var newtime = moment(apptime).format('h:mm a');
appointmentArr[newdate].forEach(function(value, key) {
console.log(value); //these are all the appointment times for 04/04/2016 eg 11:22:00 AM
});
My question is, how can i check with moment.js, if "newtime" and "value" are within XX mins of each other? ive looked in the docs and on other posts but cant get it right with the AM PM formatting i am using.
To compare two dates with MomentJS you can use moment.diff, and you can specify the unit of measure you want your result in:
newDate.diff(oldDate, 'minutes');
You can then compare that value with your threshold and profit!
EDIT: But you should not format the date before comparing it, since format returns a string. Keep your moment object intact, and format it whenever you need to display it (or save the formatted string in another variable).

How to get time using Moment JS

I've two javascript variables: startDate and endDate. I want to store the current time(unix timestamp in ms) to endDate and timestamp of 24 hours before in startDate. I'll use this two variables to query the records for last 1 day. How can I do that using moment JS?
Have you looked at the website yet? It's full of examples - http://momentjs.com/ I think what you are trying to do is as simple as
var startDate = moment(endDate).subtract(1, 'days');
Following your question more literally, you can do this:
var endDate = moment(); //the current time
Or, you can just ignore the endDate part of this problem and go straight to startDate with
var startDate = moment().subtract(1, 'days'); //one day before the current time
Finally, if you need to format it a certain way, you can apply a formatting rule such as:
moment().subtract(1,'days').format('YYYY-MM-DD h:mm:ss a')
Use format without an argument and it gives you ISO 8601 format
moment().subtract(1,'days').format() //eg: "2015-04-04T01:53:26-05:00"
This worked for me although I have never found it in the docs. Should have been published but it works.
Try:
moment(currentTime).format("hh:mm"));or
var currentTime = moment();
console.log("CURRENT TIME: " + moment(currentTime).format("hh:mm"));
For those who are looking for a way to get timestamp, just do it:
moment().valueOf()
I think what you are looking for is something like
moment(endDate).unix()
which returns something like:
1435161240
You can even calculate the time from now using
moment(endDate).fromNow()
which returns something like:
"2 days ago"
You can directly call function momentInstance.valueOf(), it will return numeric value of time similar to date.getTime() in native java script.

Compare different time zone date in Jquery

How to compare dates from different time zone?
for e.g.
x = "2013-12-02T10:10:17-0400" // (timezone EST)
and compare this date to current date
var d = new Date(); // timezone(PST)
check x < d ?
When it comes to dealing with dates and times in JavaScript, I usually use Moment.js which is a library exactly for this purpose.
Its URL is http://momentjs.com/
Then you can simply parse the given string with this line:
// Parse the given datetime
var mydate = moment("2013-12-02T10:10:17-0400");
And you can also compare two different moment values:
// Compare given datetime with the current datetime
if (moment("2013-12-02T10:10:17-0400") > moment()) {
// ...
}
Or you can just convert it to a regular JavaScript Date object:
// Parse given datetime and convert to Date object
var mydate = moment("2013-12-02T10:10:17-0400").toDate();
// Compare to current datetime
if (mydate > (new Date())) {
// ...
}
Note that the unary + operator also works with moment objects just as you would expect. So +moment() outputs the same as +(new Date()).
It's also very well documented, the Moment.js docs page has a ton of examples and useful info about it.
Better convert any of the dates to a common timezone(better to have UTC)
Now convert the datetime to milliseconds
Compare the milliseconds
Hope you understand
Use this javascript library to manipulate dates in different time zones: https://github.com/mde/timezone-js
It uses the TZ database: http://en.wikipedia.org/wiki/Tz_database

Categories