@Wangww0925
2019-08-07T07:55:49.000000Z
字数 1377
阅读 225
js-Date()
注意:单位是毫秒
PS:由于使用了jquery的$.extend()
,所以需要引入jquery.js
/** 根据给定的时间戳,返回【时分秒】
* 参数
time 时间戳,单位 毫秒
hSplit 时的类型 (如 时 || :)
mSplit 分的类型 (如 分 || :)
sSplit 秒的类型 (如 秒 || 空"")
symbol 给定时间戳小于0的前缀符号
isSec true-过滤秒
isZero true-时分秒小于10添加0
* 调用
timestamp({
time: new Date().getTime() - 1000 * 60 * 56 * 2 - new Date().getTime(),
hSplit: "时",
mSplit: "分",
sSplit: "秒",
symbol: "超时",
isSec: true
});
*/
function timestamp(options){
var option = {
hSplit: ":",
mSplit: ":",
sSplit: " ",
symbol: "超时",
isZero: true,
isSec: false
}
$.extend(option, options);
var date = Math.abs(option.time); // 将时间转化为正数
// 将时间差 转换成 天 时 分 秒
var d = parseInt(date / (1000 * 60 * 60 * 24)); // 天
var h = parseInt(date / (1000 * 60 * 60) - d * 24); // 时
d > 0 ? h = h + d * 24 : ""
var m = parseInt(date / (1000* 60) - d * 24 * 60 - h * 60); // 分
var s = parseInt(date / 1000 - d * 24 * 60 * 60 - h * 60 * 60 - m * 60); // 秒
if (option.isZero){
h < 10 ? h = "0" + h : "";
m < 10 ? m = "0" + m : "";
s < 10 ? s = "0" + s : "";
}
return (option.time > 0 ? "" : option.symbol) + (h != "00" ? h + option.hSplit : "" ) + m + option.mSplit + (option.isSec ? '' : s + option.sSplit);
}
timestamp({time: new Date().getTime() + 1000 * 60 * 56 - new Date().getTime(), symbol: "-"}); // 56:00
timestamp({time: new Date().getTime() - 1000 * 60 * 56 - new Date().getTime(), symbol: "-"}); // -56:00
timestamp({
time: new Date().getTime() - 1000 * 60 * 56 * 2 - new Date().getTime(),
hSplit: "时",
mSplit: "分",
sSplit: "秒",
isZero: false,
symbol: "超时"
}); // 超时1时52分0秒
timestamp({
time: new Date().getTime() - 1000 * 60 * 56 * 2 - new Date().getTime(),
hSplit: "时",
mSplit: "分",
sSplit: "",
symbol: "超时",
isSec: true
}); // 超时01时52分
作者 wendy
2019 年 7月 30日