zz Python datetime / time conversions

Python datetime / time conversions

Date: 2008-11-12  |   Tags: datetimepython   |   3 Comments

from datetime import datetime
import time

#-------------------------------------------------
# conversions to strings
#-------------------------------------------------
# datetime object to string
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
print date_str

# time tuple to string
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print date_str

#-------------------------------------------------
# conversions to datetime objects
#-------------------------------------------------
# time tuple to datetime object
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
dt_obj = datetime(*time_tuple[0:6])
print repr(dt_obj)

# date string to datetime object
date_str = "2008-11-10 17:53:59"
dt_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(dt_obj)

# timestamp to datetime object in local time
timestamp = 1226527167.595983
dt_obj = datetime.fromtimestamp(timestamp)
print repr(dt_obj)

# timestamp to datetime object in UTC
timestamp = 1226527167.595983
dt_obj = datetime.utcfromtimestamp(timestamp)
print repr(dt_obj)

#-------------------------------------------------
# conversions to time tuples
#-------------------------------------------------
# datetime object to time tuple
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
time_tuple = dt_obj.timetuple()
print repr(time_tuple)

# string to time tuple
date_str = "2008-11-10 17:53:59"
time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(time_tuple)

# timestamp to time tuple in UTC
timestamp = 1226527167.595983
time_tuple = time.gmtime(timestamp)
print repr(time_tuple)

# timestamp to time tuple in local time
timestamp = 1226527167.595983
time_tuple = time.localtime(timestamp)
print repr(time_tuple)

#-------------------------------------------------
# conversions to timestamps
#-------------------------------------------------
# time tuple in local time to timestamp
time_tuple = (2008, 11, 12, 13, 59, 27, 2, 317, 0)
timestamp = time.mktime(time_tuple)
print repr(timestamp)

#-------------------------------------------------
# results
#-------------------------------------------------
# 2008-11-10 17:53:59
# 2008-11-12 13:51:18
# datetime.datetime(2008, 11, 12, 13, 51, 18)
# datetime.datetime(2008, 11, 10, 17, 53, 59)
# datetime.datetime(2008, 11, 12, 13, 59, 27, 595983)
# datetime.datetime(2008, 11, 12, 21, 59, 27, 595983)
# (2008, 11, 10, 17, 53, 59, 0, 315, -1)
# (2008, 11, 10, 17, 53, 59, 0, 315, -1)
# (2008, 11, 12, 21, 59, 27, 2, 317, 0)
# (2008, 11, 12, 13, 59, 27, 2, 317, 0)
# 1226527167.0
时间: 2024-11-05 11:46:07

zz Python datetime / time conversions的相关文章

python datetime模块用strftime 格式化时间

python datetime模块用strftime 格式化时间 ? Python 三大神器 这是最后一篇 #!usr/bin/python import datetime datetime.datetime.now() 这个会返回 microsecond.因此这个是我们不需要的.所以得做一下修改 datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 格式化之后,就得到了我们常见的格式了. 附:strftime参数 strftime

[zz]Python 3.4 and Pillow 2.4 with JPEG2000 (openjpeg 2) support in Ubuntu 14.04

Pillow support for JPEG2000 comes from openjpeg 2 library. Unfortunatly, Ubuntu's libopenjpeg2 package is not version 2 of the openjpeg library, but actually version 1.3 which wont work with Pillow. Installing Pillow 2.4 (pip install -I pillow) simpl

python datetime模块用法

python datetime模块用法 1. 创建naive(无时区信息)的datetime对象 import datetime dt_utc = datetime.datetime.utcnow() dt_utc # datetime.datetime(2019, 2, 1, 10, 53, 34, 145034) dt_utc.tzinfo # None dt_local = datetime.datetime.now() dt_local.tzinfo # None # tzinfo为No

python datetime和calendar模块常用功能

一,datetime模块 datetime模块定义了下面这几个类:datetime.date:表示日期的类.常用的属性有year, month, day:datetime.time:表示时间的类.常用的属性有hour, minute, second, microsecond:datetime.datetime:表示日期和时间.datetime.timedelta:表示时间间隔,即两个时间点之间的长度.datetime.tzinfo:与时区有关的相关信息. 1,datetime.datetime对

python datetime模块的日常使用

[10:52:43][[email protected]| ~]# python Python 2.7.12 (default, Jun 27 2017, 11:19:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>

python datetime 写入mysql datetime类型

最近有个python项目需要更新mysql时间,类型是datetime,参考了网上的建议,将时间转为字符串再写入数据库. def toMysqlDateTime(): dt=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")    return dt

python datetime处理时间

Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接口则更直观.更容易调用.今天就来讲讲datetime模块. datetime模块定义了两个常量:datetime.MINYEAR和datetime.MAXYEAR,分别表示datetime所能表示的最 小.最大年份.其中,MINYEAR = 1,MAXYEAR = 9999

Python datetime与timestamp之间的转换

在用Python处理datetime和timestamp的转换时发现在时区方面,Python的处理是个大坑. 因为Python的time是默认localtime输入来处理的,导致脚本在本地运行和在服务器运行会得到不一样的结果.一不注意就会中招. R中也会碰到时区的问题,但是R的方法提供了tz选项来指定日期的时区,简化了问题.而Python 中的time.mktime无法对时区进行指定,带来了很多麻烦.查了很久,发现有一个time.timezone的属性,可以根据运行脚本的机器的时间,来灵活处理时

python datetime模块参数详解

Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接口则更直观.更容易调用. datetime模块定义了两个常量:datetime.MINYEAR和datetime.MAXYEAR,分别表示datetime所能表示的最 小.最大年份.其中,MINYEAR = 1,MAXYEAR = 9999. datetime模块定义了下面这几个类: datetim