你可以试试这样。
var pd = new Date(postDate);
var nd = new Date();
var Hours = nd.getHours() - pd.getHours();
var Days = nd.getDay() - pd.getDay();
var Months = nd.getMonth() - pd.getMonth();
var Years = nd.getYear() - pd.getYear();
或获得毫秒差异
var miliseconds = (nd - pd).getTime(); //gets time in miliseconds since 1/1/1970
然后用你的逻辑来计算小时、天、月和年
你可以看看这个Work with a time span in Javascript
moment
ar date1 = new Date("7/Nov/2012 20:30:00"); var date2 = new Date("20/Nov/2012 19:15:00");
var diff = date2.getTime() - date1.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24)); 差异 -= 天 * (1000 * 60 * 60 * 24);
var hours = Math.floor(diff / (1000 * 60 * 60)); 差异 -= 小时 * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60)); 差异 -= 分钟 * (1000 * 60);
var seconds = Math.floor(diff / (1000)); 差异 -= 秒 * (1000);
console.log(天+"天:"+小时+"小时:"+分钟+"分钟:"+秒+"秒");