最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavascriptJqueryMoment.js calculate the number of days between two dates - Stack Overflow

programmeradmin3浏览0评论

I've googled loads and read loads, and so far wasted about 3 hours on this. I can't believe its so tough.

I have a Javascript/Jquery app, I have the moment.js plugin installed. I'm writing a POS application, and I need to calculate the difference in days between two dates, so I can warn the user that a particular returned item might be too old to be returned.

I found this code in JS which looks good and seems to be the popular way to do it, although I just couldn't get it to work

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.round(Math.abs((firstDate.getTime() -  secondDate.getTime())/(oneDay)));

I also tried this using Moment.js, which again looks really neat

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var x = a.diff(b);

The latter would be my preferred technique. But in my case I get the error "oDate.diff is not a function", here's my code ...

todaysDate = moment(new Date()).format('YYYY, MM, DD');
oDate = moment(result.Order.created).format('YYYY, MM, DD');
var diffDays = oDate.diff(todaysDate, 'days');

I suspect the problem is to do with the format of the oDate variable. But I can't work out why.

EDIT. Incidentally I checked the value of todaysDate and oDate with console.log and they are todaysDate - 2016, 09, 14 oDate - 2016, 09, 12

Any advice? Cheers

I've googled loads and read loads, and so far wasted about 3 hours on this. I can't believe its so tough.

I have a Javascript/Jquery app, I have the moment.js plugin installed. I'm writing a POS application, and I need to calculate the difference in days between two dates, so I can warn the user that a particular returned item might be too old to be returned.

I found this code in JS which looks good and seems to be the popular way to do it, although I just couldn't get it to work

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.round(Math.abs((firstDate.getTime() -  secondDate.getTime())/(oneDay)));

I also tried this using Moment.js, which again looks really neat

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var x = a.diff(b);

The latter would be my preferred technique. But in my case I get the error "oDate.diff is not a function", here's my code ...

todaysDate = moment(new Date()).format('YYYY, MM, DD');
oDate = moment(result.Order.created).format('YYYY, MM, DD');
var diffDays = oDate.diff(todaysDate, 'days');

I suspect the problem is to do with the format of the oDate variable. But I can't work out why.

EDIT. Incidentally I checked the value of todaysDate and oDate with console.log and they are todaysDate - 2016, 09, 14 oDate - 2016, 09, 12

Any advice? Cheers

Share Improve this question asked Sep 14, 2016 at 10:29 Robin WalmsleyRobin Walmsley 951 gold badge2 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

Do no use format function before taking the difference. . .once you call format it wont be moment object anymore. format function converts it into a string.

todaysDate = moment(new Date());
oDate = moment(result.Order.created);
var diffDays = oDate.diff(todaysDate, 'days');

Did you try something like this?

var startDate = new Date(2008, 01, 12);
var endDate = new Date(2008, 01, 22);
var duration = moment.duration(endDate.diff(startDate));
var diffDays = duration.asDays();

Here I use moment.duration() and diff() to get the interval between the two dates duration, and then asDays() to get the result in days...

You could convert date to unix timestamp (moment.unix()), subtract two timestamps and check duration using moment.duration(), it's similar to your first try

You can try this:

function showDays(firstDate,secondDate){



              var startDay = new Date(firstDate);
              var endDay = new Date(secondDate);
              var millisecondsPerDay = 1000 * 60 * 60 * 24;

              var millisBetween = startDay.getTime() - endDay.getTime();
              var days = millisBetween / millisecondsPerDay;

              // Round down.
              alert( Math.floor(days));

          }
发布评论

评论列表(0)

  1. 暂无评论