找回密码
 立即注册
首页 业界区 业界 时区转换工具+PWA离线网页

时区转换工具+PWA离线网页

酒跚骼 2025-6-2 22:39:10
时区转换工具+PWA离线网页

一、时区转换工具对比

工具说明Date原生 JS API,有限的时区支持,无法指定时区,仅使用本地时区。Intl.DateTimeFormat原生格式化显示,可指定时区,但不能修改时区逻辑。luxon强烈推荐,现代、轻量、功能强,原生支持时区、时间戳、格式化等。dayjs + timezone 插件类似 moment,更现代,但时区支持需插件。moment-timezone功能全面但体积大,moment 官方已不推荐用于新项目。二、Luxon 使用示例

1. 美国时间 -> 中国时间
  1. import { DateTime } from 'luxon'
  2. const usTime = DateTime.fromISO('2025-04-01T11:11:00', { zone: 'America/Los_Angeles' })
  3. const timestamp = usTime.toMillis()
  4. const cnTime = usTime.setZone('Asia/Shanghai')
  5. console.log('美国时间:', usTime.toFormat('yyyy-MM-dd HH:mm:ss ZZZZ'))
  6. console.log('时间戳:', timestamp)
  7. console.log('对应的中国时间:', cnTime.toFormat('yyyy-MM-dd HH:mm:ss ZZZZ'))
复制代码
  1. 美国时间:2025-04-01 11:11:00 GMT-7
  2. 时间戳:1743521460000
  3. 对应的中国时间:2025-04-02 02:11:00 GMT+8
复制代码
2. 中国时间 -> 美国时间
  1. const cn = DateTime.fromISO('2025-04-01T11:11:00', { zone: 'Asia/Shanghai' })
  2. const us = cn.setZone('America/Los_Angeles')
  3. console.log('中国时间:', cn.toFormat('yyyy-MM-dd HH:mm:ss ZZZZ'))
  4. console.log('对应的美国时间:', us.toFormat('yyyy-MM-dd HH:mm:ss ZZZZ'))
  5. console.log('时间戳(UTC):', cn.toMillis())
复制代码
3. 转换逻辑总结

场景方法指定时区的时间 → 时间戳DateTime.fromISO(...).toMillis()时间戳 → 指定时区时间DateTime.fromMillis(...).setZone(...)不同时区之间转换.setZone(...)时间格式化.toFormat('yyyy-MM-dd HH:mm:ss') 等4. 常用时区 ID 表

名称IANA 时区 ID北京/上海(Asia/Shanghai)Asia/Shanghai香港(Asia/Hong_Kong)Asia/Hong_Kong日本(Asia/Tokyo)Asia/Tokyo韩国(Asia/Seoul)Asia/Seoul新加坡(Asia/Singapore)Asia/Singapore印度(Asia/Kolkata)Asia/Kolkata美国西部 - 洛杉矶(America/Los_Angeles)America/Los_Angeles美国中部 - 芝加哥(America/Chicago)America/Chicago美国东部 - 纽约(America/New_York)America/New_York英国(Europe/London)Europe/London德国(Europe/Berlin)Europe/Berlin法国(Europe/Paris)Europe/Paris澳大利亚 - 悉尼(Australia/Sydney)Australia/Sydney新西兰(Pacific/Auckland)Pacific/Auckland夏威夷(Pacific/Honolulu)Pacific/HonoluluUTC(协调世界时)UTC三、时区转换脚本

1. NodeJS 脚本(使用 luxon)
  1. const { DateTime } = require('luxon')
  2. function convertTime({
  3.   timeStr = '2025-04-01 11:11:00',
  4.   fromZone = 'America/Los_Angeles',
  5.   toZone = 'Asia/Shanghai'
  6. }) {
  7.   const fromTime = DateTime.fromFormat(timeStr, 'yyyy-MM-dd HH:mm:ss', { zone: fromZone })
  8.   const toTime = fromTime.setZone(toZone)
  9.   console.log(`原始时间 (${fromZone}):`, fromTime.toFormat('yyyy-MM-dd HH:mm:ss ZZZZ'))
  10.   console.log(`时间戳(UTC 毫秒):`, fromTime.toMillis())
  11.   console.log(`转换后 (${toZone}):`, toTime.toFormat('yyyy-MM-dd HH:mm:ss ZZZZ'))
  12. }
  13. // 修改这里的参数即可
  14. convertTime({
  15.   timeStr: '2025-04-01 11:11:00',
  16.   fromZone: 'America/Los_Angeles',
  17.   toZone: 'Asia/Shanghai'
  18. })
复制代码
2. Python 脚本(使用 pytz)
  1. pip install pytz
复制代码
  1. from datetime import datetime
  2. import pytz
  3. def convert_time(time_str='2025-04-01 11:11:00', from_zone='America/Los_Angeles', to_zone='Asia/Shanghai'):
  4.     from_tz = pytz.timezone(from_zone)
  5.     to_tz = pytz.timezone(to_zone)
  6.     naive_dt = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
  7.     from_dt = from_tz.localize(naive_dt)
  8.     to_dt = from_dt.astimezone(to_tz)
  9.     print(f'原始时间 ({from_zone}): {from_dt.strftime("%Y-%m-%d %H:%M:%S %Z%z")}')
  10.     print(f'时间戳(UTC 秒): {int(from_dt.timestamp())}')
  11.     print(f'转换后 ({to_zone}): {to_dt.strftime("%Y-%m-%d %H:%M:%S %Z%z")}')
  12. # 修改参数即可
  13. convert_time(
  14.     time_str='2025-04-01 11:11:00',
  15.     from_zone='America/Los_Angeles',
  16.     to_zone='Asia/Shanghai'
  17. )
复制代码
四、网页小工具

使用 Luxon + HTML 原生控件制作的小工具,支持:

  • 输入时间
  • 原始/目标时区选择
  • 时间戳显示
  • 一键复制
1. 代码展示

[code]   
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册