You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中生成每周时间戳并转换为UNIX时间戳

实现方案

完全可以一步完成,你可以直接用Python标准库的datetime模块实现,不需要额外安装第三方依赖。
以下是可直接运行的代码示例:

from datetime import datetime, timedelta

# 配置参数
start_time = datetime(2021, 1, 7, 12, 0, 0)  # 自定义起始时间
generate_count = 10  # 生成的时间戳总数量,可自行修改

for i in range(generate_count):
    current_time = start_time + timedelta(weeks=i)
    # 生成你需要的格式化时间字符串
    format_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
    # 转换为10位UNIX时间戳(单位:秒)
    unix_time = int(current_time.timestamp())
    # 可直接打印输出,也可以按需存入列表/文件
    print(f"{format_time} | UNIX时间戳:{unix_time}")

运行后输出效果如下:

2021-01-07 12:00:00 | UNIX时间戳:1610001600
2021-01-14 12:00:00 | UNIX时间戳:1610606400
2021-01-21 12:00:00 | UNIX时间戳:1611211200
...

补充说明:

  • 如果需要13位毫秒级的UNIX时间戳,把int(current_time.timestamp())改成int(current_time.timestamp() * 1000)即可
  • 生成的所有时间可以直接存入列表做后续处理,不用分两次生成

内容的提问来源于stack exchange,提问作者MathMan 99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 23:36:04