216 lines
5.4 KiB
JavaScript
216 lines
5.4 KiB
JavaScript
|
|
"use strict";
|
||
|
|
cc._RF.push(module, '3d716bsy3xD/JwwTxxyLvbB', 'bridge.time');
|
||
|
|
// Scripts/zbridge/utils/bridge.time.js
|
||
|
|
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// ================================================================
|
||
|
|
//
|
||
|
|
// 桥接 时间快捷接口
|
||
|
|
//
|
||
|
|
// ================================================================
|
||
|
|
|
||
|
|
// 时间秒数
|
||
|
|
var SecsMinute = 60;
|
||
|
|
var SecsHour = SecsMinute * 60;
|
||
|
|
var SecsDay = SecsHour * 24;
|
||
|
|
var SecsWeek = SecsDay * 7;
|
||
|
|
|
||
|
|
// 导出
|
||
|
|
module.exports = {
|
||
|
|
// 本地化日期字符串
|
||
|
|
// _time: 秒级时间戳
|
||
|
|
// _odate: 输出日期 2023/6/9
|
||
|
|
// _otime: 输出时间 14:26:11
|
||
|
|
// 完整格式: 2023/6/9 14:26:11
|
||
|
|
toLocalString: function toLocalString(_time, _odate, _otime) {
|
||
|
|
if (_odate === void 0) {
|
||
|
|
_odate = true;
|
||
|
|
}
|
||
|
|
if (_otime === void 0) {
|
||
|
|
_otime = true;
|
||
|
|
}
|
||
|
|
// 两位数修正
|
||
|
|
var two = function two(_val) {
|
||
|
|
var val = "" + Math.floor(_val);
|
||
|
|
return val.padStart(2, "0");
|
||
|
|
};
|
||
|
|
var s1 = "";
|
||
|
|
var s2 = "";
|
||
|
|
|
||
|
|
// 输出日期
|
||
|
|
var time = new Date(_time * 1000);
|
||
|
|
if (_odate) {
|
||
|
|
s1 = time.getFullYear() + "/" + two(time.getMonth() + 1) + "/" + two(time.getDate());
|
||
|
|
if (!_otime) {
|
||
|
|
return s1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 输出时间
|
||
|
|
if (_otime) {
|
||
|
|
s2 = two(time.getHours()) + ":" + two(time.getMinutes()) + ":" + two(time.getSeconds());
|
||
|
|
if (!_odate) {
|
||
|
|
return s2;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 完整日期
|
||
|
|
return s1 + " " + s2;
|
||
|
|
},
|
||
|
|
// 本地化月日
|
||
|
|
// _time: 秒级时间戳
|
||
|
|
// 返回: N月M日
|
||
|
|
toLocalMD: function toLocalMD(_time) {
|
||
|
|
var time = new Date(_time * 1000);
|
||
|
|
return nx.text.format("SMD", time.getMonth() + 1, time.getDate());
|
||
|
|
},
|
||
|
|
// 早先日期的描述
|
||
|
|
// _time: 秒级时间戳
|
||
|
|
// 返回: xxx之前
|
||
|
|
toDescAgo: function toDescAgo(_time) {
|
||
|
|
var now = new Date();
|
||
|
|
var time = new Date(_time * 1000);
|
||
|
|
if (time > now) {
|
||
|
|
nx.warn("接口调用错误,请检查!");
|
||
|
|
return this.toLocalString(_time);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 超过一年
|
||
|
|
var dt = Math.floor((now - time) / 1000);
|
||
|
|
var days = dt / SecsDay;
|
||
|
|
var years = now.getFullYear() - time.getFullYear();
|
||
|
|
if (days > 350 && years > 0) {
|
||
|
|
// 几年前
|
||
|
|
if (years < 3) {
|
||
|
|
return nx.text.format("AgoSYear", nx.text.getKey("Num" + years));
|
||
|
|
}
|
||
|
|
// 很久以前
|
||
|
|
return nx.text.getKey("AgoLongTime");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 刚刚
|
||
|
|
var minutes = dt / SecsMinute;
|
||
|
|
if (minutes < 1) {
|
||
|
|
return nx.text.getKey("AgoJustNow");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 未满一小时
|
||
|
|
var hours = dt / SecsHour;
|
||
|
|
if (hours < 1) {
|
||
|
|
return nx.text.format("AgoSMinute", Math.floor(minutes));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 未满一天
|
||
|
|
if (days < 1) {
|
||
|
|
return nx.text.format("AgoSHour", Math.floor(hours));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 未满一周
|
||
|
|
var weeks = dt / SecsWeek;
|
||
|
|
if (weeks < 1) {
|
||
|
|
return nx.text.format("AgoSDay", Math.floor(days));
|
||
|
|
}
|
||
|
|
if (weeks > 1) {
|
||
|
|
return nx.text.format("AgoSDay", Math.floor(days));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 详细时间
|
||
|
|
return this.toLocalString(_time);
|
||
|
|
},
|
||
|
|
// 所需时间描述
|
||
|
|
// _seconds: 总秒数
|
||
|
|
// 返回: xx天xx小时xx分xx秒
|
||
|
|
toNeedSeconds: function toNeedSeconds(_seconds) {
|
||
|
|
// 无效
|
||
|
|
if (!nx.dt.numPositive(_seconds, false)) {
|
||
|
|
return nx.text.format("SSecond", 0);
|
||
|
|
}
|
||
|
|
var days = _seconds / SecsDay;
|
||
|
|
var hours = _seconds % SecsDay / SecsHour;
|
||
|
|
var mins = _seconds % SecsHour / SecsMinute;
|
||
|
|
var secs = _seconds % SecsMinute;
|
||
|
|
var txt = "";
|
||
|
|
if (days >= 1) {
|
||
|
|
txt += nx.text.format("SDays", Math.floor(days));
|
||
|
|
}
|
||
|
|
;
|
||
|
|
if (hours >= 1) {
|
||
|
|
txt += nx.text.format("SHour", Math.floor(hours));
|
||
|
|
}
|
||
|
|
;
|
||
|
|
if (days < 1) {
|
||
|
|
txt += nx.text.format("SMinute", Math.floor(mins));
|
||
|
|
}
|
||
|
|
;
|
||
|
|
if (hours < 1) {
|
||
|
|
txt += nx.text.format("SSecond", Math.floor(secs));
|
||
|
|
}
|
||
|
|
;
|
||
|
|
return txt || nx.text.format("SSecond", 0);
|
||
|
|
},
|
||
|
|
// 所需时间描述
|
||
|
|
// _time: 秒级时间戳
|
||
|
|
// 返回: xx天xx小时xx分xx秒
|
||
|
|
toNeedTime: function toNeedTime(_time) {
|
||
|
|
var now = Math.floor(cc.sys.now() / 1000);
|
||
|
|
return this.toNeedSeconds(_time - now);
|
||
|
|
},
|
||
|
|
// 时间倒计时
|
||
|
|
// _seconds: 总秒数
|
||
|
|
// 返回: xx:xx:xx
|
||
|
|
cdSeconds: function cdSeconds(_seconds) {
|
||
|
|
// 无效
|
||
|
|
if (!nx.dt.numPositive(_seconds, false)) {
|
||
|
|
return "00:00:00";
|
||
|
|
}
|
||
|
|
var hours = _seconds / SecsHour;
|
||
|
|
var mins = _seconds % SecsHour / SecsMinute;
|
||
|
|
var secs = _seconds % SecsMinute;
|
||
|
|
var two = function two(_val) {
|
||
|
|
var val = "" + Math.floor(_val);
|
||
|
|
return val.padStart(2, "0");
|
||
|
|
};
|
||
|
|
return nx.text.format("%s:%s:%s", two(hours), two(mins), two(secs));
|
||
|
|
},
|
||
|
|
// 日期倒计时
|
||
|
|
// _time: 秒级时间戳
|
||
|
|
// 返回: xx:xx:xx
|
||
|
|
cdDate: function cdDate(_time) {
|
||
|
|
var now = Math.floor(cc.sys.now() / 1000);
|
||
|
|
return this.cdSeconds(_time - now);
|
||
|
|
},
|
||
|
|
// 返回简化倒计时
|
||
|
|
cdSimple: function cdSimple(_time) {
|
||
|
|
// 已经过期
|
||
|
|
var now = Math.floor(cc.sys.now() / 1000);
|
||
|
|
var secs = _time - now;
|
||
|
|
if (secs <= 0) {
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
// 天数
|
||
|
|
var days = secs / SecsDay;
|
||
|
|
if (days > 1) {
|
||
|
|
return nx.text.format("SDay", Math.ceil(days));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 小时
|
||
|
|
var hours = secs % SecsDay / SecsHour;
|
||
|
|
if (hours > 1) {
|
||
|
|
return nx.text.format("SHour", Math.ceil(hours));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 分钟
|
||
|
|
var mins = secs % SecsHour / SecsMinute;
|
||
|
|
if (mins > 1) {
|
||
|
|
return nx.text.format("SMinute", Math.ceil(mins));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 秒
|
||
|
|
secs = secs % SecsMinute;
|
||
|
|
return nx.text.format("SSecond", secs);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
cc._RF.pop();
|