本文内容不是解决问题,而是记录一些常用跟时间有关的方法,代码用的是TS语言。
第一个:传入秒数,返回时分秒(字符串,格式00:00:00)
public static TimeSecondToHMS(seconds: number): string {
// 时
let hour: number = Math.floor(seconds / 60 / 60);
// 剩余秒数
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;
};
第二个:传入秒数,返回分秒(字符串,格式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;
}
第三个:获得当前时间戳(秒级)
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;
}