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

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();
  }

}

posted @ 2017-07-02 16:44  焦大叔  阅读(1132)  评论(0编辑  收藏  举报