【问题标题】:Get weekend timestamp in python在python中获取周末时间戳
【发布时间】:2016-08-28 04:54:23
【问题描述】:

我必须将获取周末时间戳从 php 转换为 python。我谷歌了很多次,但找不到答案。 在php中我可以做

endOfWeek = strtotime('next saturday, 11:59:59pm America/Los_Angeles');

你们知道如何在 python 中做类似的事情吗?请帮助我

【问题讨论】:

    标签: python timestamp


    【解决方案1】:

    您可以使用dateutil 包。

    from dateutil.relativedelta import *
    from datetime import *
    from dateutil.tz import *
    today = datetime.now(tzutc())
    new_time = today+relativedelta(weekday=FR, hours=11, minutes=59, seconds=00)
    new_time = new_time.astimezone(gettz("America/Los Angeles"))
    print(new_time)
    

    输出:

    2016-09-02 02:19:21.899007-05:00

    【讨论】:

      【解决方案2】:

      @何塞 是的,谢谢。我在当地看到了我的工作。但我们计划在 Google App Engine 上使用 Python,它不允许使用扩展库。但我发现如何使用标准日期、时间模块

          year_no = datetime.datetime.utcnow().isocalendar(0)
          week_no = datetime.datetime.utcnow().isocalendar()[1] + 1  # next week
          next_monday_datetime = time.strptime(year_no + ' ' + week_no + ' ' + '1', '%Y %W %w')  # monday of next week date
          # get 23:59:59 of previous sunday timestamp for back-ward compatible with old stack
          end_of_week_refer = time.mktime(next_monday_datetime) - 1
      

      虽然我还没有将它更改为美国/洛杉矶时区,但应该很容易,我已经得到周末时间戳

      【讨论】:

        猜你喜欢
        • 2018-10-11
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 2018-12-26
        • 2019-06-26
        • 2022-01-06
        • 2019-06-05
        • 1970-01-01
        相关资源
        最近更新 更多