时间戳与各种时间格式之间的转换

var common={

  /*时间戳与的转换
  */
  //获得当前时间戳的三种方式
  _timestemp:function(){
    return Date.parse(new Date())/1000;//=>1498983745

    //二:new Date().valueOf();/=>1498983687749

    //三:new Date().getTime();//=>1498983687749
  },
  //时间戳转换成时间: 2011-3-16 16:50:43 (这种格式有一个缺点就是格式不统一,可能出现2011/3/16 下午16:50:43这种格式,这是由LocalString引起的)
  _getDateHasHorizontalLine:function(timestemp){
    return new Date(parseInt(timestemp) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
  },
  //时间戳转换成时间: 2011/3/16 16:50:43 (缺点同上)
  _getDateHasSlash:function(timestemp){
    return new Date(parseInt(timestemp) * 1000).toLocaleString().replace("-","/").replace("-","/");
  },
  //时间戳转换成时间: 2011年3月16日 16:50:43(这个常用,以免在不同的设备上格式不统一)
  _getDateHasHoursMinutesSecondes:function(timestemp){
    var time=new Date(parseInt(timestemp) * 1000),
      timeFormat,
      minutes,
      seconds;
     minutes=time.getMinutes();
     seconds=time.getSeconds();
    if(minutes<10){
      minutes="0"+time.getMinutes();
    }
    if(seconds<10){
      seconds="0"+time.getSeconds();
    }
    timeFormat=time.getFullYear()+"年"
        +(time.getMonth()+1)+"月"
        +time.getDate()+"日"+" "
        +time.getHours()+":"
        +minutes+":"
        +seconds;
    return   timeFormat;
  },
  //时间戳转换成时间: 2011-3-16
  _getDateNoMilliSecond:function(timestemp){
    return new Date(parseInt(timestemp) * 1000).toLocaleDateString();
  }

}

转载于:https://www.cnblogs.com/tiny-jiao/p/7106398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值