(1)Intl.NumberFormat 是对语言敏感的格式化数字类的构造器类(参考Intl.NumberFormat)
(2)padStart(当前字符串需要填充到的目标长度, 填充字符串)—在原字符串开头填充指定的填充字符串直到目标长度所形成的新字符串。(参考padStart)
// 获取本地时间
import { ref } from 'vue';
export const useTime = () => {
const year = ref<number | string>(0);
const month = ref<number | string>(0);
const week = ref<string>("");
const day = ref<number | string>(0);
const hour = ref<number | string>(0);
const minute = ref<number | string>(0);
const second = ref<number | string>(0);
const nowTime = ref<string>("");
// 更新时间
const updateTime = () => {
const date = new Date();
year.value = date.getFullYear();
month.value = date.getMonth() + 1;
// getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天
week.value = "日一二三四五六".charAt(date.getDay());
day.value = date.getDate();
// Intl.NumberFormat 是对语言敏感的格式化数字类的构造器类
// padStart(当前字符串需要填充到的目标长度, 填充字符串)---在原字符串开头填充指定的填充字符串直到目标长度所形成的新字符串。
hour.value = (date.getHours() + "").padStart(2, "0") || new Intl.NumberFormat(undefined, { minimumIntegerDigits: 2 }).format(date.getHours())
minute.value = (date.getMinutes()+ "").padStart(2, "0") || new Intl.NumberFormat(undefined, { minimumIntegerDigits: 2 }).format(date.getMinutes())
second.value = (date.getSeconds() + "").padStart(2, "0") || new Intl.NumberFormat(undefined, { minimumIntegerDigits: 2 }).format(date.getSeconds())
nowTime.value = `${year.value}年${month.value}月${day.value} ${hour.value}:${minute.value}:${second.value}`
}
updateTime()
return { year, month, week, day, minute, second, hour, nowTime }
}