JavaScript 时间与Unix时间戳互相转换,指定时间转换或获取当前时间
工作上用到这个还挺多的,做个简单的随笔,以后查询的时候比较方便:
一般JS获取的时间戳默认是13位数字,在个人的工作中,PHP后台只需要精确到秒就行,看各位亲的需要了,
代码注释里尽量写的比较清楚了 <( ̄︶ ̄)↗[GO!]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>时间与时间戳互相转换</title>
</head>
<body>
<script>
//---------时间转换时间戳
//1、转换指定的时间
//声明一个正常页面上显示的时间:
let data1="2018-02-16 12:11:11";
//返回的是13位毫秒的数值
let time1=new Date(Date.parse(data1.replace(/-/g, "/"))).getTime();
//不过一般后台传输时只需要精确到秒就可以了,再除以1000即可
document.write("时间转换时间戳 : <br/> time1 : "+time1/1000 +" <br/> ");
//2、转换当前时间:
let data2=new Date();
let time2=Number(data2); //精确到毫秒
let time3=Date.parse(data2); //虽然也是到毫秒,不过毫秒数并不准确,以000显示
let time4= +data2; //精确到毫秒
document.write("time2: "+time2+" <br/> time3: "+time3+" <br/> time4: "+time4 +" <br/> ");
//---------时间戳转换页面显示的时间格式
//括号里填写指定的时间戳 不填默认当前的时间戳
let data3 = new Date(1518754271 * 1000); //精确到毫秒的就不用 *1000 了
let y=data3.getFullYear(),
month=data3.getMonth() + 1,
m=month<10 ? "0"+month : month,
d=(data3.getDate()<10) ? "0"+data3.getDate() : data3.getDate(),
h=(data3.getHours()<10) ? "0"+data3.getHours() : data3.getHours(),
mi=(data3.getMinutes()<10) ? "0"+data3.getMinutes() : data3.getMinutes(),
s=(data3.getSeconds()<10) ? "0"+data3.getSeconds() : data3.getSeconds();
sendDate=y + "-" +m + "-" +d + " " +h + ":" +mi + ":" +s;
document.write(" <br/> 时间转换时间戳 : <br/> sendDate: "+sendDate);
</script>
</body>
</ht
ml>
(不支持ECMAScript 6 语法 把let 换成 var 就行,)
显示数据:
ps: 复制用毫秒数:
一小时:3600000
一天:86400000
还没有评论,来说两句吧...