python 时间操作
来源:互联网 发布:tcp 发送syslog java 编辑:程序博客网 时间:2024/05/21 19:25
转自 http://www.cnblogs.com/xuchunlin/p/5920702.html
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)
Python 实现得到现在时间12个月前的每个月
假设现在的时间是2016年9月25日
得到现在的时间 得到now等于2016年9月25日
now = datetime.datetime.now()
得到今年的的时间 (年份) 得到的today_year等于2016年
today_year = now.year
今年的时间减去1,得到去年的时间。last_year等于2015
last_year = int(now.year) -1
得到今年的每个月的时间。today_year_months等于1 2 3 4 5 6 7 8 9,
today_year_months = range(1,now.month+1)
得到去年的每个月的时间 last_year_months 等于10 11 12
last_year_months = range(now.month+1, 13)
定义列表去年的数据
data_list_lasts = []
通过for循环,得到去年的时间夹月份的列表
先遍历去年每个月的列表
for last_year_month in last_year_months:
# 定义date_list 去年加上去年的每个月
date_list = ‘%s-%s’ % (last_year, last_year_month)
#通过函数append,得到去年的列表
data_list_lasts.append(date_list)
data_list_todays = []
通过for循环,得到今年的时间夹月份的列表
先遍历今年每个月的列表
for today_year_month in today_year_months:
# 定义date_list 去年加上今年的每个月
data_list = ‘%s-%s’ % (today_year, today_year_month)
#通过函数append,得到今年的列表
data_list_todays.append(data_list)
去年的时间数据加上今年的时间数据得到年月时间列表
data_year_month = data_list_lasts + data_list_todays
data_year_month.reverse()
- [Python] Python时间操作
- python中的时间操作
- python常用时间操作
- python常用时间操作
- python时间操作
- python时间操作
- python 时间操作
- Python时间操作
- python 时间操作集合
- python 时间操作
- python时间操作
- python 时间操作
- python时间操作
- python时间操作
- python时间日期操作
- [python]时间操作
- python 时间操作
- python时间操作
- Head first JavaScript_#1简单用户交互界面
- NYOJ 17-单调递增最长子序列(典型DP)
- 561. Array Partition I
- WebStorm下使用Github上传项目代码
- 38条PHP优化代码!让你的程序更简洁!
- python 时间操作
- iOS 最简单解决事件冲突的思路,深入浅出cancelsTouchesInView属性
- OrientDB社区版Linux分布式部署失败
- 使聊天机器人具有个性
- 强化学习及Python代码示例
- HTML-标签语义化
- c语言结构体学习时写的小代码
- 对指令集的一点理解
- 2017 CBDS第四届中国国际大数据大会9月26日北京召开