python时间转换字符串格式
1. python怎么把时间戳转换成字符串
#设a为字符串
import time
a = "2011-09-28 10:00:00"
#中间过程,一般都需要将字符串转化为时间数组
time.strptime(a,'%Y-%m-%d %H:%M:%S')
>>time.struct_time(tm_year=2011, tm_mon=9, tm_mday=27, tm_hour=10, tm_min=50, tm_sec=0, tm_wday=1, tm_yday=270, tm_isdst=-1)
#将"2011-09-28 10:00:00"转化为时间戳
time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S'))
>>1317091800.0
#将时间戳转化为localtime
x = time.localtime(1317091800.0)
time.strftime('%Y-%m-%d %H:%M:%S',x)
>>2011-09-27 10:50:00
2. python 字符串转时间
1、说明:python使用datetime模块中datetime.datetime.strptime()函数来将字符串转换成时间。
2、示例代码:
import datetime
print(datetime.datetime.strptime('11:47','%M:%S'))
输出结果:
1900-01-01 00:11:47
strptime(string, format) -> datetime
根据格式规范解析字符串到时间。
格式化字符说明:
%Y 年份以世纪为十进制数。
%m 月份的十进制数[01,12]。
%d 当月日为十进制数[01,31]。
%H 小时(24小时制)作为十进制数[00,23]。
%M 分钟的十进制数[00,59]。
%S 其次为十进制数[00,61]。
%z 时区与UTC的偏移。
%a 本机格式的缩写工作日名称。
%A 本机格式的完整周日名称。
%b 本机格式的缩写月份名称。
%B 本机格式的完整月份名称。
%c 本机格式的适当的日期和时间表示。
%I 小时(12小时制)作为十进制数[01,12]。
%p 对语言环境的等同无论是上午或下午。
3. python中,怎么把字符串转换为日期格式
python中要把字符串转换成日期格式需要使用time模块中的strptime函数,例子如下:
1
2
3
import time
t = time.strptime('2016-05-09 21:09:30', '%Y-%m-%d %H:%M:%S')
print(t)
执行结果如下:
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=9, tm_hour=21, tm_min=9, tm_sec=30, tm_wday=0, tm_yday=130, tm_isdst=-1)
函数说明:
第一个参数是要转换成日期格式的字符串,第二个参数是字符串的格式
函数官方文档如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Help on built-in function strptime in mole time:
strptime(...)
strptime(string, format) -> struct_time
Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as
strftime()).
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
Other codes may be available on your platform. See documentation for the C library strftime function.
4. python 如何将字符串转化为datetime.date
比较省事的办法是用time模块的strptime方法来解析日期字符串成为时间对象,然后再把年月日部分提取出来,最后生成datetime.date对象。
#方法1,用time模块的strptime方法来解析日期字符串成为时间对象
importtime,datetime
date_str='2017-10-19'
fmt='%Y-%m-%d'
time_tuple=time.strptime(date_str,fmt)
year,month,day=time_tuple[:3]
a_date=datetime.date(year,month,day)
print(a_date,type(a_date))
#方法2,直接把日期字符串拆分转换成年/月/日对应的整数
importdatetime
date_str='2017-10-19'
print(datetime.date(*map(int,date_str.split('-'))))
5. python 字符串转时间
1、说明:
python使用datetime模块中datetime.datetime.strptime()函数来将字符串转换成时间。
2、示例代码:
import
datetime
print(datetime.datetime.strptime('11:47','%M:%S'))
输出结果:
1900-01-01
00:11:47
3、函数说明:
strptime(string,
format)
->
datetime
根据格式规范解析字符串到时间。
格式化字符说明:
%Y
年份以世纪为十进制数。
%m
月份的十进制数[01,12]。
%d
当月日为十进制数[01,31]。
%H
小时(24小时制)作为十进制数[00,23]。
%M
分钟的十进制数[00,59]。
%S
其次为十进制数[00,61]。
%z
时区与UTC的偏移。
%a
本机格式的缩写工作日名称。
%A
本机格式的完整周日名称。
%b
本机格式的缩写月份名称。
%B
本机格式的完整月份名称。
%c
本机格式的适当的日期和时间表示。
%I
小时(12小时制)作为十进制数[01,12]。
%p
对语言环境的等同无论是上午或下午。
6. python 时间的格式转化,格式为20130415172500字符串转为datetime类型
python编程用datetime方法进行时间转换,代码如下:
$python
Python2.7.2+(default,Jul202012,22:12:53)
[gcc4.6.1]onlinux2
Type"help","right","credits"or"license"formoreinformation.
>>>importdatetime
>>>dtstr="20130415172559"
>>>dt=datetime.datetime.strptime(dtstr,"%Y%m%d%H%M%S")
>>>dt
datetime.datetime(2013,4,15,17,25,59)
>>>another_dt=dt+datetime.timedelta(seconds=2)
>>>another_dt
datetime.datetime(2013,4,15,17,26,1)
>>>
7. python中,有个字符串形式的时间戳,如何转换为日期呢
用datetime.datetime.strptime()函数;
a=datetime.datetime.strptime('2018-1-8 10:10:10','%Y-%m-%d %H:%M:%S')
输出的a就是时间格式
8. python中,怎么把字符串转换为日期格式
用Python实现字符串和日期相互转换的方法,具体如下:
这里用的分别是time和datetime函数来处理
import
time,datetime//日期转化为字符串#
date
to
str//输出时间print
time.strftime("%Y-%m-%d
%X",
time.localtime())#str
to
date//字符串转化为日期t
=
time.strptime("2016
-
12
-
05",
"%Y
-
%m
-
%d")y,m,d
=
t[0:3]//输出时间print
datetime.datetime(y,m,d)
9. python中,怎么把字符串转换为日期格式
1、python中要把字符串转换成日期格式需要使用time模块中的strptime函数,例子如下:
10. python中,怎么把字符串转换为日期格式
1、新建python文件,testtime.py;