转自:http://www.2cto.com/kf/201401/276088.html
1 .将字符串的时间转换为时间戳 方法: a "2013-10-10 将其转换为时间数组 import
timeArray "%Y-%m-%d ) 转换为时间戳: timeStamp int (time.mktime(timeArray)) timeStamp 1381419600 |
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
2 .字符串格式更改 如a "2013-10-10 ,想改为 "2013/10/10 方法:先转换为时间数组,然后转换为其他格式 timeArray "%Y-%m-%d ) otherStyleTime "%Y/%m/%d , 3 .时间戳转换为指定格式日期: 方法一: 利用localtime()转换为时间数组,然后格式化为需要的格式,如 timeStamp 1381419600 timeArray otherStyleTime "%Y-%m-%d , otherStyletime "2013-10-10 方法二: import
timeStamp 1381419600 dateArray otherStyleTime "%Y-%m-%d ) otherStyletime "2013-10-10 4 .获取当前时间并转换为指定日期格式 方法一: import
获得当前时间时间戳 now int (time.time()) 转换为其他日期格式,如: "%Y-%m-%d timeArray otherStyleTime "%Y-%m-%d , 方法二: import
获得当前时间 now 转换为指定的格式: otherStyleTime "%Y-%m-%d ) 5 .获得三天前的时间 方法: import
import
先获得时间数组格式的日期 threeDayAgo 3 )) 转换为时间戳: timeStamp int (time.mktime(threeDayAgo.timetuple())) 转换为其他字符串格式: otherStyleTime "%Y-%m-%d ) 注:timedelta()的参数有:days,hours,seconds,microseconds 6 .给定时间戳,计算该时间的几天前时间: timeStamp 1381419600 先转换为datetime import
import
dateArray threeDayAgo 3 ) 参考 5 ,可以转换为其他的任意格式了 |