在Python中从int到datetime [英] int to datetime in Python

查看:75
本文介绍了在Python中从int到datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从端口接收数据.

I'm receiving data from the port.

此数据具有日期和时间.但未格式化.

This data has date and time.But not formatted.

示例:

'02012019', '090253'

我想将其保存到数据库中,并且要转换这种格式.

I want to save it to the database and I want to convert this format.

02-01-2019   09:02:53

是否有此功能?还是我写这个有问题吗?它会在5秒钟内运行200次.

Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.

有人有建议吗?

推荐答案

from datetime import datetime

date_data  ='02012019 09:02:53' 
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')

# output '02-01-2019 09:02:53'

这篇关于在Python中从int到datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆