前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 时间 今日,昨天, 周, 月

Python 时间 今日,昨天, 周, 月

作者头像
Wyc
发布2021-07-01 18:01:52
6480
发布2021-07-01 18:01:52
举报
文章被收录于专栏:Python研发
代码语言:javascript
复制
# 今日
import time

today_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
代码语言:javascript
复制
# 昨天
import datetime

cur_date = datetime.datetime.now().date()
yester_day = cur_date - datetime.timedelta(days=1)
代码语言:javascript
复制
# 周
import datetime

def get_current_week():
    monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day
    return str(monday), str(sunday)
代码语言:javascript
复制
# 月
from datetime import datetime

currentYear = datetime.now().year  # 年
currentMonth = datetime.now().month  # 月
currentDay = datetime.now().day  # 天
year_month = str(currentYear) + "-" + str(currentMonth) + "-" + "01 00:00:00"
year_month_day = str(currentYear) + "-" + str(currentMonth) + "-" + str(currentDay) + " 23:59:59"
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档