本文內(nèi)容不是解決問題,而是記錄一些常用跟時間有關(guān)的方法,代碼用的是TS語言蔬墩。
第一個:傳入秒數(shù)右蒲,返回時分秒(字符串怪得,格式00:00:00)
public static TimeSecondToHMS(seconds: number): string {
// 時
let hour: number = Math.floor(seconds / 60 / 60);
// 剩余秒數(shù)
let left_second: number = seconds - hour * 60 * 60;
// 分
let minute: number = Math.floor(left_second / 60);
// 秒
let second: number = left_second - minute * 60;
// 時間串
let str: string = "";
if (hour < 10) {
str += "0";
}
str += hour + ":";
if (minute < 10) {
str += "0";
}
str += minute + ":";
if (second < 10) {
str += "0";
}
str += second;
return str;
};
第二個:傳入秒數(shù),返回分秒(字符串,格式00:00)
public static FormatTimeStr(seconds: number): string {
let str: string = "";
let minute: number = Math.floor(seconds / 60);
let second: number = seconds - minute * 60;
if (minute < 10) {
str += "0";
}
str += minute + ":";
if (second < 10) {
str += "0";
}
str+= second;
return str;
}
第三個:獲得當(dāng)前時間戳(秒級)
public static GetCurrentTimeStampSecond(): number {
let time: number = Date.parse(new Date().toString());
return Math.floor(time / 1000);
}
第四個:獲得本周周一零點、周日二十四點的時間戳
public static GetMondayTimeStamp(): number {
let currentTimeStamp: number = Tools.GetCurrentTimeStampSecond();
let currentDate: Date = new Date(currentTimeStamp * 1000);
let dayNum: number = (currentDate.getDay() == 0) ? 7: currentDate.getDay();
return (currentTimeStamp - (dayNum - 1) * 24 * 60 * 60 - currentDate.getHours() * 60 * 60 - currentDate.getMinutes() * 60 - currentDate.getSeconds());
}
public static GetSundayTimeStamp(): number {
return Tools.GetMondayTimeStamp() + 7 * 24 * 60 * 60 - 1;
}