/**
* 格式化日期選項
*/
export class DateFormatOption {
"M+": number;//月
"d+": number;//日
"H+": number;//小時
"m+": number;//分
"s+": number;//秒
"q+": number;//季度
"S+": number;//毫秒
}
export class Utility {
constructor() {
}
/**
* 格式化日期
* @param date 日期
* @param fmt 格式化字符串
* @returns 字符串
*/
static formatDate(date: Date, fmt: string) {
const options = new DateFormatOption();
options["M+"] = date.getMonth() + 1;
options["d+"] = date.getDate();
options["H+"] = date.getHours();
options["m+"] = date.getMinutes();
options["s+"] = date.getSeconds();
options["q+"] = Math.floor((date.getMonth() + 3) / 3);
options["S+"] = date.getMilliseconds();
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (const i in options) {
const key = i as keyof DateFormatOption;//轉換key格式
if (new RegExp("(" + i + ")").test(fmt)) {
let matchZeros="";//補零
for (let j = 0; j < RegExp.$1.length; j++) {
matchZeros+="0";
}
const newVal = (matchZeros + options[key]).substr(("" + options[key]).length);
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (options[key]).toString() : newVal);
}
}
return fmt;
}
/**
* 獲取時間
* @param timeStamp 時間戳
* @returns Date對象
*/
static getDate(timeStamp: number | string) {
const unixTimestamp = new Date(timeStamp);
return unixTimestamp;
}
/**
* 獲取格式化日期的字符串
* @param timeStamp Unix時間戳
* @returns 字符串
*/
static getDateTimeStr(timeStamp: number | string, format: string) {
const date = this.getDate(timeStamp);
if (!format) {
format = "yyyy-MM-dd HH:mm:ss";
}
const result = this.formatDate(date, format)
return result;
}
/**
* 獲取格式化日期的字符串,最小單位ms
* @param timeStamp Unix時間戳
* @returns 字符串
*/
static getDateTimeMsStr(timeStamp: number | string) {
const date = this.getDate(timeStamp);
const format = "yyyy-MM-dd HH:mm:ss.SSS";
const result = this.formatDate(date, format)
return result;
}
/**
* 獲取年齡
* A DateTime extension method that ages the given this.
* 醫用周歲
* 【年齡=(當前年份-出生年份)+ (當前月日>出生月日?1:0) -1】
* @param timeStamp Unix時間戳
*/
static getAge(timeStamp: number) {
const date = this.getDate(timeStamp);
const now = new Date();
if (now.getMonth() < date.getMonth() ||
now.getMonth() == date.getMonth() &&
now.getDay() < date.getDay()) {
return now.getFullYear() - date.getFullYear() - 1
}
return now.getFullYear() - date.getFullYear();
}
/**
* 獲取BMI指數
* BMI=體重/身高的平方(國際單位kg/㎡)
* @param height 身高cm
* @param weight 體重kg
*/
static getBmi(height: number, weight: number) {
const bmi = weight / Math.pow(height / 100, 2);
return bmi;
}
}