python中的日期时间分割 [英] Date Time split in python

查看:30
本文介绍了python中的日期时间分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将我从以下格式的软件中获得的日期时间拆分为单独的变量(年、月、日、小时、分钟、秒)

2015 年 11 月 19 日 18:45:00.000

注意:日期和时间之间有两个空格.整个日期和时间存储在单个字符串变量中.请在这方面帮助我.

提前致谢.

解决方案

以下解决方案应该适合您:

导入日期时间字符串 = "2015 年 11 月 19 日 18:45:00.000"date = datetime.datetime.strptime(string, "%d %b %Y %H:%M:%S.%f")打印日期

输出将是:

2015-11-19 18:45:00

您可以通过以下方式访问所需的值:

<预><代码>>>>日期.年份2015年>>>日期.月份11>>>日期.天19>>>日期.小时18>>>日期.分钟45>>>日期.秒0

您可以在 datetime 的包文档://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior" rel="noreferrer">第 8.1.7 节 srtptime 函数的使用.

I have to split a date time which I get from a software in the below format to separate variables (year,month,day,hour, min,sec)

19 Nov 2015  18:45:00.000

Note : There is two spaces between the date and time. The whole date and time is stored in a single string variable. Please help me in this regards.

Thanks in advance.

解决方案

Below solution should work for you:

import datetime

string = "19 Nov 2015  18:45:00.000"
date = datetime.datetime.strptime(string, "%d %b %Y  %H:%M:%S.%f")

print date

Output would be:

2015-11-19 18:45:00

And you can access the desired values with:

>>> date.year
2015
>>> date.month
11
>>> date.day
19
>>> date.hour
18
>>> date.minute
45
>>> date.second
0

You can check datetime's package documentation under section 8.1.7 for srtptime function's usage.

这篇关于python中的日期时间分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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