javascript - 如何在 JavaScript 中计算两个日期之间的天数

标签 javascript

如何使用 JavaScript 计算两个日期之间的天数?

  1. How to get only 'day' from the date.
  2. How to get only 'month' from the date.
  3. How to get only 'Year' from the date.
  4. How to calculate 'year' from the 'days'

此类问题都有答案,请引用

最佳答案

您可以使用Moment.Js获取两个日期之间的差异,如下所示。

var start = moment("2015-13-08");
var end = moment("2015-13-08");
start.diff(end, "days")

要获取日、月、年,您可以使用 native javaScript。

var k = new Date();
k.getDate(); //gets the date
k.getMonth() + 1 ;  //gets the month index (0-11) starting from 0, so u will get 7 
                    //  for August. Added +1 to get the  month index on 1-12  scale
k.getFullYear() ; //gets the full year.

//Year from days
 var years =  days/365 ;  //days is the no of days.

关于javascript - 如何在 JavaScript 中计算两个日期之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984739/

相关文章:

javascript - 计算容器中旋转矩形的 Angular

javascript - websocket.js 导致 React 应用程序意外刷新

javascript - 为什么这个最小公分母函数返回 undefined 对于更高的数字输入

javascript - 了解初学者的循环冗余码算法

javascript - Google Charts - 尝试在弹出窗口中添加链接或任何形式的自定义 html

javascript - 如何重置 setInterval 函数内的值?

javascript - 数组:有条件地映射元素

javascript - 切换 'this' 内另一个元素的类

javascript - 如何使用 javascript 控制 block 数量的呈现

javascript - Angular.js 单元测试 - 麻烦模拟 $window