// ================================================================ // // 桥接 时间快捷接口 // // ================================================================ // 时间秒数 const SecsMinute = 60; const SecsHour = SecsMinute * 60; const SecsDay = SecsHour * 24; const SecsWeek = SecsDay * 7; // 导出 module.exports = { // 本地化日期字符串 // _time: 秒级时间戳 // _odate: 输出日期 2023/6/9 // _otime: 输出时间 14:26:11 // 完整格式: 2023/6/9 14:26:11 toLocalString: function( _time, _odate = true, _otime = true ) { // 两位数修正 let two = function( _val ) { let val = "" + Math.floor( _val ); return val.padStart( 2, "0" ); }; let s1 = ""; let s2 = ""; // 输出日期 let 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( _time ) { let time = new Date( _time * 1000 ); return nx.text.format( "SMD", time.getMonth() + 1, time.getDate() ); }, // 早先日期的描述 // _time: 秒级时间戳 // 返回: xxx之前 toDescAgo: function( _time ) { let now = new Date(); let time = new Date( _time * 1000 ); if( time > now ) { nx.warn( "接口调用错误,请检查!" ); return this.toLocalString( _time ); } // 超过一年 let dt = Math.floor( ( now - time ) / 1000 ); let days = dt / SecsDay; let 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" ); } // 刚刚 let minutes = dt / SecsMinute; if( minutes < 1 ) { return nx.text.getKey( "AgoJustNow" ); } // 未满一小时 let 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 ) ); } // 未满一周 let 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( _seconds ) { // 无效 if( !nx.dt.numPositive( _seconds, false ) ) { return nx.text.format( "SSecond", 0 ); } let days = _seconds / SecsDay; let hours = ( _seconds % SecsDay ) / SecsHour; let mins = ( _seconds % SecsHour ) / SecsMinute; let secs = _seconds % SecsMinute; let 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( _time ) { let now = Math.floor( cc.sys.now() / 1000 ); return this.toNeedSeconds( _time - now ); }, // 时间倒计时 // _seconds: 总秒数 // 返回: xx:xx:xx cdSeconds: function( _seconds ) { // 无效 if( !nx.dt.numPositive( _seconds, false ) ) { return "00:00:00"; } let hours = _seconds / SecsHour; let mins = ( _seconds % SecsHour ) / SecsMinute; let secs = _seconds % SecsMinute; let two = function( _val ) { let 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( _time ) { let now = Math.floor( cc.sys.now() / 1000 ); return this.cdSeconds( _time - now ); }, // 返回简化倒计时 cdSimple: function( _time ) { // 已经过期 let now = Math.floor( cc.sys.now() / 1000 ); let secs = _time - now; if( secs <= 0 ) { return ""; } // 天数 let days = secs / SecsDay; if( days > 1 ) { return nx.text.format( "SDay", Math.ceil( days ) ); } // 小时 let hours = ( secs % SecsDay ) / SecsHour; if( hours > 1 ) { return nx.text.format( "SHour", Math.ceil( hours ) ); } // 分钟 let mins = ( secs % SecsHour ) / SecsMinute; if( mins > 1 ) { return nx.text.format( "SMinute", Math.ceil( mins ) ); } // 秒 secs = secs % SecsMinute; return nx.text.format( "SSecond", secs ); }, }