I am trying to see if any of the dates in an object are within the last 3 days of the current date using moment.
The keys of the object are dates, but I am not sure how to use moment to pare them to the current date.
Edit: The reason I want to find dates within the last 3 days is to get the number of issues found within that date range. I think I can do this if I can just get a boolean flag to determine if an object in the store falls within that date range.
Here's a link to a fiddle I made: /
var currentDate = moment().format('YYYY-MM-DD');
var store = {
"2015-05-20": {
"issues": 1
},
"2015-05-18": {
"issues": 2
},
"2015-05-17": {
"issues": 3
},
"2015-05-16": {
"issues": 1
}
};
console.log(currentDate, store);
for (var prop in store) {
if ( store.hasOwnProperty(prop) ) {
console.log(prop);
console.log( moment().diff(currentDate, 'days') > 3 );
}
}
I am trying to see if any of the dates in an object are within the last 3 days of the current date using moment.
The keys of the object are dates, but I am not sure how to use moment to pare them to the current date.
Edit: The reason I want to find dates within the last 3 days is to get the number of issues found within that date range. I think I can do this if I can just get a boolean flag to determine if an object in the store falls within that date range.
Here's a link to a fiddle I made: http://jsfiddle/yx22qqvz/
var currentDate = moment().format('YYYY-MM-DD');
var store = {
"2015-05-20": {
"issues": 1
},
"2015-05-18": {
"issues": 2
},
"2015-05-17": {
"issues": 3
},
"2015-05-16": {
"issues": 1
}
};
console.log(currentDate, store);
for (var prop in store) {
if ( store.hasOwnProperty(prop) ) {
console.log(prop);
console.log( moment().diff(currentDate, 'days') > 3 );
}
}
Share
Improve this question
edited May 20, 2015 at 16:46
Mdd
asked May 20, 2015 at 16:32
MddMdd
4,45012 gold badges48 silver badges71 bronze badges
1
- I don't know about moment, but I think the simplest and most efficient way to achieve that is to read current timestamp with (new Date()).getTime(), substract 3 × 24 × 60 × 60 × 1000 and pare with the same operation (except substraction) over each timestamp. – bitifet Commented May 20, 2015 at 16:47
2 Answers
Reset to default 5Consider:
// keep this as a moment, and use noon to avoid DST issues
var currentDate = moment().startOf('day').hour(12);
...
// parse the property at noon also
var m = moment(prop + "T12:00:00");
// diff the current date (as a moment), against the specific moment
console.log(currentDate.diff(m, 'days') > 3);
I've also updated your fiddle.
In your old code, you were just paring the current moment (moment()
) against currentDate
, which is always going to be 0 days. You have to parse the property in question to pare that bit of data.
Using noon will avoid the issue where in some time zones and some browsers, midnight of the day of a DST transition gets adjusted back to 23:00 on the previous day. Since you're working with whole dates, it is safer to explicitly use noon instead of midnight.
I guess you want
function some_within_three_days(store) {
var three_days_ago = moment().subtract(3, 'days');
function within_three_days(date) { return moment(date) . isAfter(three_days_ago); }
return Object.keys(store) . some(within_three_days);
}
or something similar depending on your exact requirements.