40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
export const useDate = () => {
|
||
const hours = ref("00");
|
||
const minutes = ref("00");
|
||
const seconds = ref("00");
|
||
const year = ref("");
|
||
const month = ref("");
|
||
const day = ref("");
|
||
const weekday = ref(""); // 新增星期几变量
|
||
|
||
let timer: number | null = null;
|
||
|
||
const updateTime = () => {
|
||
const now = new Date();
|
||
|
||
hours.value = now.getHours().toString().padStart(2, "0");
|
||
minutes.value = now.getMinutes().toString().padStart(2, "0");
|
||
seconds.value = now.getSeconds().toString().padStart(2, "0");
|
||
|
||
year.value = now.getFullYear().toString();
|
||
month.value = (now.getMonth() + 1).toString().padStart(2, "0");
|
||
day.value = now.getDate().toString().padStart(2, "0");
|
||
|
||
// 获取星期几并转换为中文格式
|
||
const weekdayNumber = now.getDay(); // 0是星期日,1-6是星期一至星期六
|
||
const weekdayNames = ["日", "一", "二", "三", "四", "五", "六"];
|
||
weekday.value = "星期" + weekdayNames[weekdayNumber];
|
||
};
|
||
|
||
return {
|
||
hours,
|
||
minutes,
|
||
seconds,
|
||
year,
|
||
month,
|
||
day,
|
||
weekday, // 返回星期几
|
||
timer,
|
||
updateTime,
|
||
};
|
||
}; |