针对时间存储,timedelta内部只能存储days,seconds,microseconds,
其他参数的值会自动按如下规则进行转换:
1 millisecond(毫秒)转换成 1000 microseconds(微秒)
1 minute 转换成 60 seconds
1 hour 转换成 3600 seconds
1 week转换成 7 days
from datetime import datetime
from datetime import timedelta
str1="2020-09-10 10:34:02.244"
st1 = datetime.strptime(str1, '%Y-%m-%d %H:%M:%S.%f')
ed_str1 = datetime.strftime(st1,'%Y-%m-%d %H:%M:%S.%f')
print(st1,"转化",ed_str1,"长度",len(ed_str1))
str2="2020-09-10 10:34:02"
st2 = datetime.strptime(str2, '%Y-%m-%d %H:%M:%S')
print(st2,"转化",datetime.strftime(st2,'%Y-%m-%d %H:%M:%S.%f'))
str3="2020-09-10 10:34:02.0"
st3 = datetime.strptime(str3, '%Y-%m-%d %H:%M:%S.%f')
print(st3,"转化",datetime.strftime(st3,'%Y-%m-%d %H:%M:%S.%f'))
str4="2020-09-10 10:34:02.00"
st4 = datetime.strptime(str4, '%Y-%m-%d %H:%M:%S.%f')
print(st4,"转化",datetime.strftime(st4,'%Y-%m-%d %H:%M:%S.%f'))
str5="2020-09-10 10:34:02.000"
st5 = datetime.strptime(str5, '%Y-%m-%d %H:%M:%S.%f')
print(st5,"转化",datetime.strftime(st5,'%Y-%m-%d %H:%M:%S.%f'))
输出显示
2020-09-10 10:34:02.244000 转化 2020-09-10 10:34:02.244000 长度 26
2020-09-10 10:34:02 转化 2020-09-10 10:34:02.000000
2020-09-10 10:34:02 转化 2020-09-10 10:34:02.000000
2020-09-10 10:34:02 转化 2020-09-10 10:34:02.000000
2020-09-10 10:34:02 转化 2020-09-10 10:34:02.000000
from datetime import datetime
from datetime import timedelta
timestr="2020-09-10 10:34:02.244"
st = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S.%f')
ed = st+timedelta(seconds=5)
print(ed)# 2020-09-10 10:34:07.244000
print(type(ed))
# datetime转化为str
edstr = datetime.strftime(ed,'%Y-%m-%d %H:%M:%S.%f')
print(edstr)
print(type(edstr))
2020-09-10 10:34:07.244000
转化为字符串后
2020-09-10 10:34:07.244000
from datetime import datetime
from datetime import timedelta
timestr="2020-09-10 10:00:00.244"
st = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S.%f')
print(st.year)
print(st.month)
print(st.day)
print(st.hour)
print(st.minute)
print(st.second)
print(st.microsecond)
输出
2020
9
10
10
0
0
244000
import datetime
curtime = datetime.datetime.now()
curtime_str = datetime.datetime.strftime(curtime,'%Y-%m-%d %H:%M:%S.%f')[:-3]
print(curtime_str)
以下是得到总秒数,类型是float型
from datetime import datetime
from datetime import timedelta
st_str = "2020-09-10 10:34:02.244"
ed_str = "2020-09-10 10:34:02.245"
st_datetime = datetime.strptime(st_str, '%Y-%m-%d %H:%M:%S.%f')
ed_datetime = datetime.strptime(ed_str, '%Y-%m-%d %H:%M:%S.%f')
delta = ed_datetime - st_datetime
print(delta.total_seconds())
输出0.001。
import datetime
t1 = datetime.datetime(2022, 10, 27, 17, 50)
t2 = datetime.datetime(2022, 10, 28, 18, 56)
tt = t2 -t1
print(tt.total_seconds())
import datetime
curtime = datetime.datetime.now()
ts = round(curtime.timestamp()) # 时间戳秒
import datetime
# 数据排序
rr = [datetime.datetime(2022, 10, 27, 17, 56),datetime.datetime(2022, 10, 27, 17, 50),datetime.datetime(2022, 10, 25, 17, 50)]
print("排序前",rr)
rr.sort() # 升序排列
print("排序后",rr)
import time
s1 = time.time()
#程序逻辑
s2 = time.time()
print(str(s2-s1),"秒")
输出单位ms。
import time
from datetime import datetime
# 字符串时间戳转换为整数时间戳
def tostamp_int(timestr):
datetime_obj = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S.%f")
obj_stamp = int(time.mktime(datetime_obj.timetuple()) * 1000.0 + datetime_obj.microsecond / 1000.0)
return obj_stamp
timestr = "2020-06-23 10:11:37.737"
a = tostamp_int(timestr)
print(a)
print(type(a))
输出:
1592878297737
import datetime
time_str = "2022-09-27 16:42:36.358"
dt = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f")
ts_s = dt.timestamp() # 时间戳,单位秒
ts_ms = int(ts_s*1000) # 时间戳,单位毫秒
ts_us = int(ts_ms*1000) # 时间戳,单位微秒
ts_ns = int(ts_us*1000) # 时间戳,单位纳秒
print(ts_s,"秒")
print(ts_ms,"毫秒")
print(ts_us,"微秒")
print(ts_ns,"纳秒")
输出
1664268156.358 秒
1664268156358 毫秒
1664268156358000 微秒
1664268156358000000 纳秒
import time
from datetime import datetime
b = 1592878297737
def ms_tostamp(stampint):
c = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(stampint/ 1000)))
return c + "." + str(stampint)[-3:]
print(ms_tostamp(b))
输出:
2020-06-23 10:11:37.737
import datetime
ts_ms = 1664268156358 # 毫秒时间戳
time_datetime = datetime.datetime.fromtimestamp(ts_ms/1000)
time_str = datetime.strftime(time_datetime, '%Y-%m-%d %H:%M:%S.%f')
print(time_str)
输出
2022-09-27 16:42:36.358000
定义为函数
def tostamp_int(time_str):
dt = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f")
ts_s = dt.timestamp() # 时间戳,单位秒
ts_ms = int(ts_s*1000) # 时间戳,单位毫秒
return ts_ms
数据库中datetime数据类型存储的值末尾会因四舍五入出现不一致数据,影响查询结果。
比如:程序中自动获取带毫秒精度的日期’2019-03-05 01:53:55.632’,存入数据库后变成’2019-03-05 01:53:56’。
create database test;
use test;
create table t(id int,dt datetime);
insert into t values(1,'2019-03-05 01:53:55.632');
问题也好解决:
(1)修改字段类型,给datetime加上精度,改成datetime(3),这样就把后面的毫秒精度存进数据库了,也不会出现查询时数值错误。
(2)如果毫秒精度实际意义不大,可以在程序中截断毫秒值,存入数据库的值直接精确到秒,这样数据库层面不需要修改。
create table t1(id int,dt datetime(3));
insert into t1 values(1,'2019-03-05 01:53:55.632');
select * from t1;
datetime
%Y%m%d %H%m%s
YYYY-mm-dd HH:ss:mm
当写入其他时间字符串时,只有数据库能识别都会写入成功,且是想要的数据.
insert into t1 values(2,'20191221010203');
insert into t1 values(3,'2019-12-31 01:02:03.656');
insert into t1 values(4,'2019/12/31/01/02/03');
对于datetime类型的值,想要查询一天的数据,可以通过范围查询:between and(between and是左右闭合区间,两边数值都能查到) 。
select * from t1 where dt between '2019-12-21 00:00:00' and '2019-12-21 23:59:59';
import pymysql
import json
# (1)数据库信息
class config(object):
HOST = "localhost"
PORT = 3306
USERNAME = "root"
PASSWORD = "bigdata"
DBNAME = "test"
con = pymysql.Connect(host=config.HOST,
port=config.PORT,
user=config.USERNAME,
passwd=config.PASSWORD,
db=config.DBNAME)
cur = con.cursor()
sql_query = "select * from t1"
# 查询
cur.execute(sql_query)
results = cur.fetchall()
# 转化
from datetime import datetime
for re in results:
ed = re[1]
# datetime转化为str
edstr = datetime.strftime(ed,'%Y-%m-%d %H:%M:%S.%f')
print(edstr)
print(type(edstr))