dayTime.js 674 B

12345678910111213141516171819202122
  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. }