js字符串转时间戳

(1)把当前时间转成时间戳

//把时间转成时间戳
function timeTampToStr(){
   // 当前时间戳
   var timestamp = parseInt(new Date().getTime()/1000);    
   document.write(timestamp);
}

(2)当前时间换日期字符串

//把当前时间转成字符串
function currentTimeChangeStr(){
   //获取当前时间
   var now = new Date();
   //获取当前时间的年份
   var yy = now.getFullYear();      //年
   //获取当前时间的月份
   var mm = now.getMonth() + 1;     //月
   //获取当前时间的日期
   var dd = now.getDate();          //日
   //获取当前时间的小时
   var hh = now.getHours();         //时
   //获取当前时间的分
   var ii = now.getMinutes();       //分
   //获取当前时间的秒
   var ss = now.getSeconds();       //秒
   //把上述的时间转成字符串
   var clock = yy + "-";
   if(mm < 10) clock += "0";
   clock += mm + "-";
   if(dd < 10) clock += "0";
   clock += dd + " ";
   if(hh < 10) clock += "0";
   clock += hh + ":";
   if (ii < 10) clock += '0'; 
   clock += ii + ":";
   if (ss < 10) clock += '0'; 
   clock += ss;
   //获取当前日期
   document.write(clock);   
} 

(3)日期字符串转时间戳

//日期字符串转成时间戳
//例如var date = '2015-03-05 17:59:00.0';
function dateStrChangeTimeTamp(dateStr){
   dateStr = dateStr.substring(0,19);
   dateStr = dateStr.replace(/-/g,'/');
   var timeTamp = new Date(dateStr).getTime();
   document.write(timesTamp);
}

(4)时间戳转日期字符串

//把时间戳转成日期格式
//例如 timeTamp = '1425553097';
function formatTimeTamp(timeTamp){
   var time = new Date(timeTamp*1000);
   var date = ((time.getFullYear())  + "-" +
               (time.getMonth() + 1) + "-" +
               (time.getDate()) + " " +
               (time.getHour()) + ":" +
               (time.getMinutes()) + ":" +
               (time.getSeconds());
              )
   document.write(date);
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值