Skip to main content

Command Palette

Search for a command to run...

Python Python中的时间包1 datetime

Published
2 min read
Python Python中的时间包1 datetime

Python中的时间包 detetime

  • 日期与时间的结合体 -date and time
  • 获取当前时间
  • 获取时间间隔
  • 将时间对象转换成时间字符串
  • 将字符串转成时间类型

detetime包的常用功能

获取当前时间
导入包与模块
  • from datetime import datetime
  • import datetime
使用方法
  • datetime.now()
  • datetime.datetime.now() (today)

  • 返回当前年月日时分秒毫秒的datetime对象

获取时间间隔
导入包
from datetime import datetime
from datetime import timedelta
使用方法
timeobj = timedelta(days=0, seconds=0, microsenconds=0, milliseconds=0, minutes=0, hours=0, week=0)

datetime包中的常用方法

时间对象转字符串
获取对象时间
from datetime import datetime
now = datetime.datetime.now()
时间转字符串
date_str = now.strftime(format)
时间字符串转时间类型
获取时间模块
from datetime import datetime
时间字符串转时间类型
datetime.strptime(tt, format)
参数介绍
  • tt: 符合时间格式的字符串
  • format: tt时间字符串匹配规则

python的常用时间格式化符号1

字符介绍
%Y完整的年份,如2021
%m月份,1~12
%d月中的某一天(1~31)
%H一天中的第几个小时(24小时,00~23)
%I一天中的第几个小时(12小时,00~12)
%M当前的第几分(00~59)
%S当前的第几秒(0~61)闰年多占2秒
%f当前秒的第多少毫秒

python的常用时间格式化符号2

字符介绍
%a简化的星期,如星期三 Wed
%A完整的星期,如星期三 Wednesday
%b简化的月份,如二月 Fab
%B完整的月份,如二月 Fabruary
%c本地的日期和时间,如Web Fab 5 10:14:49 2020
%p显示上午还是下午,如AM代表上午,PM代表下午
%j一年中的第几天
%U一年中的星期数

代码

# coding:utf-8

from datetime import datetime
from datetime import timedelta

now = datetime.now()
print(now, type(now))
now_str = now.strftime('%Y-%m-%d %H:%M:%S')
print(now_str, type(now_str))
now_obj = datetime.strptime(now_str, '%Y-%m-%d %H:%M:%S')
print(now_obj, type(now_obj), '-----')

three_days = timedelta(days=3)
after_three_day = now + three_days
print(after_three_day)
after_three_day_str = after_three_day.strftime('%Y/%m/%d %H:%M:%S')
print(after_three_day_str, type(after_three_day_str))
after_three_day_obj = datetime.strptime(after_three_day_str, '%Y/%m/%d %H:%M:%S')
print(after_three_day_obj, type(after_three_day_obj), '-----')

before_three_day = now - three_days
print(before_three_day)
before_three_day_str = before_three_day.strftime('%Y%m%d')
print(before_three_day_str, type(before_three_day_str))
before_three_day_obj = datetime.strptime(before_three_day_str, '%Y%m%d')
print(before_three_day_obj, type(before_three_day_obj), '-------')

one_hour = timedelta(hours=1)
before_one_hour = now - one_hour
print(before_one_hour)
before_one_hour_str = before_one_hour.strftime('%H:%M:%S')
print(before_one_hour_str, type(before_three_day_str))

# default_str = '2020 12 abc'
# print(datetime.strptime(default_str, '%Y %m'))
1 views

More from this blog

MySQL | 表的内连接

数据操作语言:表连接查询(一) 从多张表中提取数据 从多张表提取数据,必须指定关联的条件。如果不定义关联条件就会出现无条件连接,两张表的数据会交叉连接,产生 笛卡尔积。 规定了连接条件的表连接语句,就不会出现笛卡尔积。 # 查询每名员工的部门信息 SELECT e.empno,e.ename,d.dname FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno; 表连接的分类 表连接分为两种:内连接 和 外连接 内连接是结果集中只保留符合...

May 16, 20221 min read13
MySQL | 表的内连接

MySQL | 分组查询的应用

数据操作语言:分组查询 为什么要分组? 默认情况下汇总函数是对全表范围内的数据做统计 GROUP BY 子句的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对每个小区域分别进行数据汇总处理 SELECT deptno,AVG(sal) FROM t_emp GROUP BY deptno; SELECT deptno,ROUND(AVG(sal)) FROM t_emp GROUP BY deptno; -- ROUND 取整 逐级分组 数据库支持多列分组条件,执行的时候...

Apr 27, 20221 min read10
MySQL | 分组查询的应用

MySQL | 聚合函数的使用

数据操作语言:聚合函数 什么是聚合函数 聚合函数在数据的查询分析中,应用十分广泛。聚合函数可以对 数据求和、求 最大值 和 最小值 、求 平均值 等等。 求公司员工的评价月收入是多少? SELECT AVG(sal+IFNULL(comm,0)) FROM t_emp; SELECT AVG(sal+IFNULL(comm,0)) AS avg FROM t_emp; SUM 函数 SUM 函数用于求和,只能用户数字类型,字符类型的统计结果为 0 ,日期类型统计结果是毫秒数相加 SE...

Apr 26, 20221 min read8
MySQL | 聚合函数的使用
U

Untitled Publication

173 posts