时间戳判断问题

JS获取当前时间戳的方法-JavaScript 获取当前毫秒时间戳有以下三种方法:  

var timestamp =Date.parse(new Date());    结果:1280977330000       //不推荐; 毫秒改成了000显示  

var timestamp =(new Date()).valueOf();       结果:1280977330748       //推荐;   

var timestamp=new Date().getTime();         结果:1280977330748        //推荐;   


js中单独调用new Date();  显示这种格式  Mar 31 10:10:43 UTC+0800 2012  

但是用new Date() 参与计算会自动转换为从1970.1.1开始的毫秒数  
function timestampFormat( timestamp ) {
    var curTimestamp = new Date().getTime(); //当前时间戳
    // console.log(curTimestamp);
    var Diff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

    var curDate = new Date( curTimestamp); // 当前时间日期对象
    var tmDate = new Date( timestamp);  // 参数时间戳转换成的日期对象

    var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
    var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();

    if ( Diff < 60*1000 ) { // 一分钟以内
        return "刚刚";
    } else if( Diff < 3600*1000 ) { // 一小时前之内
        return Math.floor( Diff / (60*1000 )) + "分钟前";
    } else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d ) {
        return Math.floor( Diff / (60*60*1000 )) + "小时前";
    } else {
        var newDate = new Date( (curTimestamp - 86400*1000)); // 参数中的时间戳加一天转换成的日期对象
        if ( newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d ) {
        return  '一天前';
        } else if (Diff>86400*1000*2){
           return new Date(timestamp);
        }
    }
}
console.log(timestampFormat(1557930299182)) //2012年01月10日 12:46
console.log(timestampFormat(1557930200000)) //刚刚
console.log(timestampFormat(Date.parse('2016-10-11 15:10:10'))) //16分钟前
console.log(timestampFormat(Date.parse('2016-10-11 10:10:10')))//今天10:10
console.log(timestampFormat(Date.parse('2016-10-10 10:10:10'))) //昨天10:10
console.log(timestampFormat(Date.parse('2016-02-10 10:10:10'))) //02月10日 10:10
console.log(timestampFormat(Date.parse('2012-10-10 10:10:10'))) //2012年10月10日 10:10

 

转载于:https://www.cnblogs.com/sarah-wen/p/10872629.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值