115 lines
2.5 KiB
TypeScript
115 lines
2.5 KiB
TypeScript
import GameUtil from "../core/GameUtil";
|
||
|
||
export default class GTimer{
|
||
static offsetTime:number=0;
|
||
|
||
static getCurTime(){
|
||
let curtime = Date.now();
|
||
curtime += GTimer.offsetTime;
|
||
return curtime;
|
||
}
|
||
|
||
static getCurDate() {
|
||
let cuttime = GTimer.getCurTime()
|
||
return new Date(cuttime);
|
||
}
|
||
|
||
static dateFormat(time:number) {
|
||
let date = new Date(time);
|
||
let y = date.getFullYear();
|
||
let m = date.getMonth() + 1;
|
||
let sm = m < 10 ? ('0' + m) : m;
|
||
let d = date.getDate();
|
||
let sd = d < 10 ? ('0' + d) : d;
|
||
let h = date.getHours();
|
||
let sh = h < 10 ? ('0' + h) : h;
|
||
let mi = date.getMinutes();
|
||
let smi = mi < 10 ? ('0' + mi) : mi;
|
||
let s = date.getSeconds();
|
||
let ss = s < 10 ? ('0' + s) : s;
|
||
return y + '-' + sm + '-' + sd + ' ' + sh + ':' + smi + ':' + ss;
|
||
}
|
||
|
||
static getTime(){
|
||
return new Date().getTime();
|
||
}
|
||
|
||
static getTimeFormat() {
|
||
let time = new Date();
|
||
return GTimer.dateFormat(time.getTime());
|
||
}
|
||
|
||
static getHours(): number {
|
||
let time = new Date();
|
||
return time.getHours(); //获取当前小时数(0-23)
|
||
}
|
||
|
||
static getWeekDay() {
|
||
let date = GTimer.getCurDate();
|
||
let t = date.getDay(); //
|
||
return t + 1;
|
||
}
|
||
|
||
static getYearDay(date:any) {
|
||
// 构造1月1日
|
||
let lastDay:any = new Date(date);
|
||
lastDay.setMonth(0);
|
||
lastDay.setDate(1);
|
||
// 获取距离1月1日过去多少天
|
||
var days = Math.ceil(date - lastDay) / (1000 * 60 * 60 * 24);
|
||
return days;
|
||
}
|
||
|
||
static getYearWeek(date:any):number{
|
||
let days = GTimer.getYearDay(date);
|
||
let num = Math.ceil(days / 7);
|
||
return num;
|
||
}
|
||
|
||
// 获取当前分钟
|
||
static getMinutes():number{
|
||
let date = new Date();
|
||
return date.getMinutes();
|
||
}
|
||
|
||
// //获取当前年份(2位)
|
||
static getCurYear() {
|
||
let date = new Date();
|
||
return date.getFullYear();
|
||
}
|
||
|
||
// 获取当前月份(0-11,0代表1月)
|
||
static getCurMonth() {
|
||
let date = new Date();
|
||
return date.getMonth() + 1;
|
||
}
|
||
|
||
// 获取当前日(1-31)
|
||
static getCurDay() {
|
||
let date = new Date();
|
||
return date.getDate();
|
||
}
|
||
|
||
static getMonthDay() {
|
||
let days = new Date(this.getCurYear(), this.getCurMonth(), 0).getDate()
|
||
return days
|
||
}
|
||
|
||
static getYearMonthDay(){
|
||
let nowDate = new Date();
|
||
let year = nowDate.getFullYear();
|
||
let month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
|
||
let day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
|
||
return year + "-" + month + "-" + day;
|
||
}
|
||
|
||
static getDateTime(str: string){
|
||
str = str.substring(0,19);
|
||
str = str.replace(/-/g,'/'); //必须把日期'-'转为'/'
|
||
return new Date(str).getTime();
|
||
}
|
||
|
||
}
|
||
|
||
|