当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript获取时间戳与时间戳转化

JavaScript获取时间戳与时间戳转化

2018年09月12日  | 移动技术网IT编程  | 我要评论

第一种方法(精确到秒):

var timestamp1 = date.parse( new date());

第二种方法(精确到毫秒): 

var timestamp2 = ( new date()).valueof();

第三种方法(精确到毫秒):

var timestamp3 = new date().gettime();

获取指定时间的时间戳: 

new date("2018-09-11 20:45:00");

时间戳转化成时间: 

  //第一种
  function getlocaltime(ns) {     
     return new date(parseint(ns) * 1000).tolocalestring().replace(/:\d{1,2}$/,' ');     
  }     
  alert(getlocaltime(1536670500));
  //结果是2018年09月11日 20:55
  //第二种    
  function getlocaltime(ns) {     
      return new date(parseint(ns) * 1000).tolocalestring().substr(0,17)
 }     
 alert(getlocaltime(1536670515));
 //第三种  格式为:2018-09-11 20:55:15
  function getlocaltime(ns) {     
        return new date(parseint(ns) * 1000).tolocalestring().replace(/年|月/g, "-").replace(/日/g, " ");      
     }     
 alert(getlocaltime(1536670515));
//第四种
 function timetrans(date){
    var date = new date(date*1000);//如果date为13位不需要乘1000
    var y = date.getfullyear() + '-';
    var m = (date.getmonth()+1 < 10 ? '0'+(date.getmonth()+1) : date.getmonth()+1) + '-';
    var d = (date.getdate() < 10 ? '0' + (date.getdate()) : date.getdate()) + ' ';
    var h = (date.gethours() < 10 ? '0' + date.gethours() : date.gethours()) + ':';
    var m = (date.getminutes() <10 ? '0' + date.getminutes() : date.getminutes()) + ':';
    var s = (date.getseconds() <10 ? '0' + date.getseconds() : date.getseconds());
    return y+m+d+h+m+s;
}

  

 

 

 

  

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网