zoukankan      html  css  js  c++  java
  • python 获取时间戳相关计算

    代码即注释,获取当前时间,当前时间上个月,当前时间下个月,当前时间上一周下一周,字符串转换时间戳

    获取当年每个月第一天,最后一天,api不清楚ilde直接查一下或者百度即可,非常简单.

    import datetime
    import time
    import calendar
    import calendar as cal

    now_day = datetime.datetime.now()

    need_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
    need_time = datetime.datetime.now().strftime('%Y-%m-%d')


    pre_week = (datetime.datetime.now() + datetime.timedelta(days=-7)).strftime('%Y-%m-%d')
    next_week = (datetime.datetime.now() + datetime.timedelta(days=7)).strftime('%Y-%m-%d')

    pre_month = (datetime.datetime.now() + datetime.timedelta(days=-30)).strftime('%Y-%m-%d')
    next_month = (datetime.datetime.now() + datetime.timedelta(days=-30)).strftime('%Y-%m-%d')

    cur_week_first_day = (now_day - datetime.timedelta(days = now_day.weekday())).strftime('%Y-%m-%d')
    cur_week_last_day = (now_day + datetime.timedelta(days = now_day.weekday())).strftime('%Y-%m-%d')

    cur_month_first_day = datetime.datetime(now_day.year, now_day.month, 1).strftime('%Y-%m-%d')
    cur_month_last_day = datetime.datetime(now_day.year, now_day.month, calendar.monthrange(now_day.year, now_day.month)[1]).strftime("%Y-%m-%d")


    print('next week: ', next_week)
    print('cur week: ', need_time)
    print('pre week: ', pre_week)

    print('cur week first day: ', cur_week_first_day)
    print('cur week last day: ', cur_week_last_day)

    print('cur month first day: ', cur_month_first_day)
    print('cur month last day: ', cur_month_last_day)

    print('need time', need_time)
    print(type(need_time))

    time_array = time.strptime(need_time, "%Y-%m-%d")
    time_stamp = time.mktime(time_array)

    print('time stamp: ', time_stamp)
    print(type(time_stamp))


    teme_format = "%d-%d-%d %d-%d-%d"
    year = 2021
    for m in range(1, 13):
    d = cal.monthrange(year, m)
    print(teme_format % (year, m, 1, year, m, d[1]))

    输出:

  • 相关阅读:
    [编程题] 基础 [位运算基础]
    [编程题] lc [191. 位1的个数]
    [编程题] lc [69. x 的平方根]
    php 中php-fpm工作原理
    redis分布式锁
    3种Redis分布式锁的对比
    php使用数据库的并发问题(乐观锁与悲观锁)
    php观察者模式应用场景实例详解
    [Usaco2008 Jan]电话网络
    关于二分图结论的一些证明
  • 原文地址:https://www.cnblogs.com/liuruoqian/p/14684543.html
Copyright © 2011-2022 走看看