@Wangww0925
2019-08-07T07:55:49.000000Z
字数 1377
阅读 270
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:00timestamp({time: new Date().getTime() - 1000 * 60 * 56 - new Date().getTime(), symbol: "-"}); // -56:00timestamp({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日
