dayTime.js 813 B

123456789101112131415161718192021222324252627282930
  1. import dayjs from "dayjs";
  2. import utc from "dayjs/plugin/utc.js"; // 导入 UTC 插件
  3. import timezone from "dayjs/plugin/timezone.js"; // 导入时区插件
  4. dayjs.extend(utc); // 加载 UTC 插件
  5. dayjs.extend(timezone); // 加载时区插件
  6. export function shanghaiTime() {
  7. // 获取当前时间
  8. const now = dayjs();
  9. // 设置为上海时区
  10. const shanghaiTime = now.tz("Asia/Shanghai");
  11. return shanghaiTime;
  12. }
  13. export function shanghaiTimeFormat(time, formatStr = "YYYY-MM-DD HH:mm:ss") {
  14. // 获取当前时间
  15. const now = dayjs(time);
  16. // 设置为上海时区
  17. const shanghaiTime = now.tz("Asia/Shanghai");
  18. return shanghaiTime.format(formatStr);
  19. }
  20. export function waittime(time = 100) {
  21. return new Promise((resolve) => {
  22. setTimeout(() => {
  23. resolve(0);
  24. }, time);
  25. });
  26. }