js或者jQuery怎么写判断时间是多久之前,

2025-05-07 03:17:50
推荐回答(2个)
回答1:








$ (function ()
    {
    gap ("2014/11/07 17:00", $ ('div'));
    })
    var gap = function (date, div)
    {
    var now = new Date;
    var that = new Date (date);
    var ms = Math.floor ((now - that) / 1000 / 60 / 60);
    if (ms > 24 && ms < 48)
    {
    div.text ('昨天 ' + that.toLocaleTimeString ());
    }
    else if (ms > 48)
    {
    div.text (that.toLocaleString ());
    }
    else
    {
    div.text (ms + ' 小时前');
    }
    }





回答2:

//你设定的时间
var aa=new Date('2014-12-27');
var y=aa.getFullYear();
var m=aa.getMonth()+1;
var d=aa.getDate();
//现在的时间
var nn=new Date();
var yn=nn.getFullYear();
var mn=nn.getMonth()+1;
var dn=nn.getDate();

alert((yn-y)+'年'+(mn-m)+'个月'+(dn-d)+'天之前')