一、JavaScript 时间转为时间戳
1.Date.now()
用Date.now()可以获得当前的时间戳
Date.now(); //1645099205343
new Date().getTime() 1645099205343
2.Date.parse() 将字符串或者时间对象直接转换为时间戳
但是不推荐这种办法,毫秒级别的数值被转化为000
Date.parse() //1645099205000
3.valueOf()
通过valueOf()函数返回指定对象的原始值获得准确的时间戳值:
(new Date()).valueOf() //1645099205343
4.getTime() 通过原型方法直接获得时间的毫秒值
new Date().getTime() //1645099205343
5.Number()
将时间对象转换为一个number 类型的数值,即时间戳
Number(new Date()) //1645099205343
二、js时间戳转时间
可以使用 new Date(时间戳) 格式转化当前时间,比如:
new Date(1645099205343)
// Thu Feb 17 2022 20:00:05 GMT+0800 (CST)
⚠️:时间戳参数必须是Number 类型,如果是字符串,解析结果:invalid Date.
如果后端之间返回时间戳给前端,下面有两种转换方式:
1.生成 ‘yyyy/MM/dd 上(下)午 hh:mm:ss ‘ 格式
function getLocalTime(n){
return new Date(parseInt(n)).toLocaleString().replace(/:\d{1,2}$/,'');
}
getLocalTime(1645099205343) //"2022/2/17 下午8:00"
也可以用如下,想取几位就几位,注意,空格也算
function getLocalTime(n) {
return new Date(parseInt(n)).toLocaleString().substr(0,14)
}
getLocalTime(1645099205343) //'2022/1/19 下午5:'
或者利用正则:
function getLocalTime(n){
return new Date(parseInt(n)).toLocaleString().replace(/年|月/g,"-").replace(/日/g,"");
}
getLocalTime(1645099205343); //"2022/2/17 下午8:00:05"
2.生成 ‘yyyy-MM-dd hh: mm:ss’. 格式
先转换为data 对象,然后利用拼接正则等手段来实现:
function getData(n){
n=new Date(n)
return n.toLocaleDateString().replace(/\//g, "-") + " " + n.toTimeString().substr(0, 8)
}
getData(1645099205343) //'2022-2-17 20:00:05'
不过这样的转换在某些浏览器上会出现不理想的效果,因为toLocaleDateString()方法因浏览器而异。
可以通过分别获取时间的年月日进行拼接,这样的兼容性更好:
function getData(n) {
let now = new Date(n),
y = now.getFullYear(),
m = now.getMonth() + 1,
d = now.getDate();
return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
}
getData(1645099205343) //'2022-2-17 20:00:05'
三、知识
1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString)
(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()
//'2022/2/18 下午6:51:09'
2.普通字符串(toDateString和toTimeString)
(new Date()).toDateString() + " " + (new Date()).toTimeString()
//"Fri Feb 18 2022 18:52:01 GMT+0800 (CST)"
3.格林威治标准时间(toGMTString)
(new Date()).toGMTString()
//"Fri, 18 Feb 2022 10:53:01 GMT"
4.Date对象字符串(toString)
(new Date()).toString()
//"Fri Feb 18 2022 18:59:35 GMT+0800 (CST)"
四、Date对象构造函数
Date对象具有多种构造函数:
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)
Date对象构造函数参数说明:
milliseconds - 距离JavaScript内部定义的起始时间1970年1月1日的毫秒数
datestring - 字符串代表的日期与时间。此字符串可以使用Date.parse()转换
year - 四位数的年份,如果取值为0-99,则在其之上加上1900
month - 0(代表一月)-11(代表十二月)之间的月份
day - 1-31之间的日期
hours - 0(代表午夜)-23之间的小时数
minutes - 0-59之间的分钟数
seconds - 0-59之间的秒数
microseconds - 0-999之间的毫秒数
Date对象返回值
let d1 = new Date();
document.write(d1.toString());
//Fri Feb 18 2022 19:05:40 GMT+0800 (CST)
let d2 = new Date("2022-1-19 17:42:33");
document.write(d2.toString());
//Fri Feb 18 2022 19:05:40 GMT+0800 (CST)Invalid Date
let d3 = new Date(2022, 1, 19);
document.write(d3.toString());
//Fri Feb 18 2022 19:05:40 GMT+0800 (CST)Invalid DateSat Feb 19 2022 00:00:00 GMT+0800 (CST)
Date做为JavaScript的一种内置对象,必须使用new的方式创建。
Date对象在JavaScript内部的表示方式是,距1970年1月1日午夜(GMT时间)的毫秒数(时间戳),我们在这里也把Date的内部表示形式称为时间戳。
可以使用getTime()将Date对象转换为Date的时间戳,方法setTime()可以把Date的时间戳转换为Date的标准形式。
Date函数使用语法
Date函数按功能分类
日期获取类函数
日期设置类函数
日期打印类函数
五、JavaScript 的时间戳 和php 的时间戳转换
js的时间戳通常是13位,php的时间戳是10位,转换函数如下:
let nowtime = (new Date).getTime();/*当前时间戳*/
/*转换时间,计算差值*/
function comptime(beginTime,endTime){
var secondNum = parseInt((endTime-beginTime*1000)/1000);//计算时间戳差值
if(secondNum>=0&&secondNum<60){
return secondNum+'秒前';
}
else if (secondNum>=60&&secondNum<3600){
var nTime=parseInt(secondNum/60);
return nTime+'分钟前';
}
else if (secondNum>=3600&&secondNum<3600*24){
var nTime=parseInt(secondNum/3600);
return nTime+'小时前';
}
else{
var nTime = parseInt(secondNum/86400);
return nTime+'天前';
}
}
t = comptime("1642471746",nowtime); //1642471746为PHP通过ajax回传的时间戳, 是10位
console.log(t); //27分钟前