python日期格式化操作
1.将字符串的时间转换为时间戳
方法:
a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S") #转换为时间戳: timeStamp = int(time.mktime(timeArray)) timeStamp == 1381419600
2.格式更改
如a = "2013-10-10 23:40:00",想改为 a = "2013/10/10 23:40:00"
方法:先转换为时间数组,然后转换为其他格式
复制代码代码如下:
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
3.时间戳转换为指定格式日期
方法一:利用localtime()转换为时间数组,然后格式化为需要的格式,如:
复制代码代码如下:
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
otherStyletime == "2013-10-10 23:40:00"
方法二:
import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
otherStyletime == "2013-10-10 23:40:00"
4.获取当前时间并转换为指定日期格式
方法一:
import time
#获得当前时间时间戳
now = int(time.time()) ->这是时间戳
#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
方法二:
import datetime
#获得当前时间
now = datetime.datetime.now() ->这是时间数组格式
#转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
5.获得三天前的时间的方法
import time
import datetime
#先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
#转换为时间戳:
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
#转换为其他字符串格式:
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
注:timedelta()的参数有:days,hours,seconds,microseconds
6.给定时间戳,计算该时间的几天前时间
timeStamp = 1381419600 #先转换为datetime import datetime import time dateArray = datetime.datetime.utcfromtimestamp(timeStamp) threeDayAgo = dateArray - datetime.timedelta(days = 3) #参考5,可以转换为其他的任意格式了
7、用Python计算昨天和明天的日期
>>> import datetime #导入日期时间模块 >>> today = datetime.date.today() #获得今天的日期 >>> print today #输出今天日期 2014-01-04 >>> yesterday = today - datetime.timedelta(days=1) #用今天日期减掉时间差,参数为1天,获得昨天的日期 >>> print yesterday 2014-01-03 >>> tomorrow = today + datetime.timedelta(days=1) #用今天日期加上时间差,参数为1天,获得明天的日期 >>> print tomorrow 2014-01-05 >>> >>> print "昨天:%s, 今天:%s, 明天:%s" % (yesterday, today, tomorrow) #字符串拼接在一起输出,这3天的日期
昨天:2014-01-03, 今天:2014-01-04, 明天:2014-01-05
8、python里使用time模块来获取当前的时间
#!/usr/bin/python
import time
print (time.strftime("%H:%M:%S"))
## 12 hour format ##
print (time.strftime("%I:%M:%S"))
#:输出
#18:11:30
#6:11:30
9、打印出当前的日期的python程序
!/usr/bin/python
import time
## dd/mm/yyyy格式
print (time.strftime("%d/%m/%Y"))
#输出:
11/03/2014
10、使用datetime模块来获取当前的日期和时间
#!/usr/bin/python
import datetime
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("ISO格式的日期和时间是 %s" % i.isoformat() )
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是 %s" %i.day)
print ("dd/mm/yyyy 格式是 %s/%s/%s" % (i.day, i.month, i.year) )
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是 %s" %i.second)
11、str:'2017年3月4日',要怎么转换为datetime
# -*- coding: utf-8 -*- from datetime import datetime time = "2017年4月2日" # 将str中的汉字换掉 time = time.replace(r'年', '-').replace(r'月', '-').replace(r'日', '') print(time) # 输出2017-4-2 print(type(time)) # <type 'str'> restime = datetime.strptime(time, '%Y-%m-%d') print(restime) # 输出结果:2017-04-02 00:00:00 print(type(restime))# <type 'datetime.datetime'>
12、任意时间字符串转换时间对象
import time
from dateutil import parser
time_string = time.ctime() # 'Thu Dec 22 10:35:25 2016',这里可以是任意的时间格式
datetime_struct = parser.parse(time_string)
print type(datetime_struct) # <type 'datetime.datetime'>
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 13:58:59
python日期格式化操作的更多相关文章
- Python 日期和时间戳的转换
Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...
- Python日期时间函数处理
所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...
- Python 日期和时间(转)
Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...
- (转)Python 日期和时间
转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- Python 日期和时间 —— datetime
Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.calendar用于处理日历相关 :time提供的接口 ...
- 【310】◀▶ Python 日期和时间
参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...
- 练习十六:Python日期格式应用(datetime)
练习:关于python日期格式应用练习.用python方法如何输出指定格式形式的日期 这里用到datetime模块,datetime模块重新封装了time模块,提供了更多接口,提供的类包括:date, ...
随机推荐
- 巨高兴,偶的文章 “如何在服务器上配置ODBC来访问本机DB2for Windows服务器”被推荐至CSDN博客首页
非常高兴,偶的文章 "如何在服务器上配置ODBC来访问本机DB2for Windows服务器"被推荐至CSDN博客首页,截图留念. 文章被推荐在C ...
- 线程ava.lang.OutOfMemoryError: unable to create new native thread
近日开发遇到多线程的问题: java.lang.OutOfMemoryError: unable to create new native thread Exception in thread &qu ...
- JeeSite
JeeSite是一个 开源的企业信息管理系统 基础框架.主要定位于"企业信息管理"领域,可用作企业信息管理类系统.网站后台管理类系统等.JeeSite是非常强调开发的高效性.健壮性 ...
- Web服务器Tomcat集群与负载均衡技术
我们曾经介绍过三种Tomcat集群方式的优缺点分析.本文将介绍Tomcat集群与负载均衡技术具体实施过程. 在进入集群系统架构探讨之前,先定义一些专门术语: 1. 集群(Cluster):是一组独立的 ...
- go 实现struct转map
从python转golang大约一个月了,对struct的使用还算顺手,但是很多时候还是会想念python的便捷.比如同时遍历两个字典,python使用for (x, y) in zip(map1, ...
- (译)WebRTC实战: STUN, TURN, Signaling
http://xiaol.me/2014/08/24/webrtc-stun-turn-signaling/ 原文:WebRTC in the real world: STUN, TURN and s ...
- Fibonacci数列使用迭代器实现
Fibonacci数列,数列中第一个数为0,第二个数为1,其后的每一个数都可由前两个数相加得到: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... class FibItera ...
- cocos2dx中的坐标系统
一. (1)OpenGL坐标系 Cocos2D-x以OpenGL和OpenGL ES为基础,所以自然支持OpenGL坐标系.该坐标系原点在屏幕左下角,x轴向右,y轴向上. (2)屏幕坐标系 屏幕坐标系 ...
- VS下使用Google Protobuf完成SOCKET通信
如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...
- ArcGIS 产品体系结构
1. 开篇 本文主要从以下几个方面介绍 ArcGIS 的产品体系 2. 详细介绍 2.1 ArcGIS Desktop 参考:[https://blog.csdn.net/hphone/article ...