一、字符串与为时间字符串之间的互相转换

  方法:time模块下的strptime方法

a = "2012-11-11 23:40:00"
# 字符串转换为时间字符串
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S") # 时间字符串转换为字符串
b = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(type(b))# <class 'str'>

二、将字符串的时间转换为时间戳

  方法:字符串 --> 时间字符串 --> 时间戳

 a = "2013-10-10 23:40:00"
# 将其转换为时间数组
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
# 转换为时间戳:
timeStamp = int(time.mktime(timeArray))
print(timeStamp)#

三、得到时间戳(10位和13位)

import time
t = time.time()
print(t) # 1436428326.207596
t_10 = int(t)# 10位时间戳
t_13 = int(round(time.time() * 1000))# 13位时间戳
print(t_10)#
print(t_13)#

四、将时间戳转换为时间格式的字符串

  方法一:利用localtime()转换为时间数组,然后格式化为需要的格式

timeStamp = 1381419600# 10位时间戳
# timeStamp_13 = 1381419600234# 13位时间戳
timeArray = time.localtime(timeStamp)# timeStamp_13 / 1000
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyletime)# "2013-10-10 23:40:00"(str)

  方法二、利用datetime模块下的utcfromtimestamp方法

import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyletime) # "2013-10-10 23:40:00"

五、时间字符串转换为时间戳

  方法:利用time模块的mktime方法

import time
import datetime
# 先获得时间数组格式的日期
test_date = datetime.datetime.now()
# 转换为时间戳:
timeStamp = int(time.mktime(test_date.timetuple()))

六、时间字符串加减日期

  方法:利用datetime模块下的timedelta方法

import time
import datetime
# 先获得时间数组格式的日期
test_datetime = datetime.datetime.now()
threeDayAgo = (test_datetime - datetime.timedelta(days = 3))# 3天前
# 注:timedelta()的参数有:days,hours,seconds,microseconds

七、获取 UTC 时间戳

import calendar
calendar.timegm(datetime.datetime.utcnow().timetuple())

八、python 格式化时间含中文报错 UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence'

import time
print(time.strftime(u'%Y年%m月%d日',time.localtime(time.time()))) # 执行上面代码会报错 UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence # 解决方式:
time.strftime('%Y{y}%m{m}%d{d}').format(y='年',m='月',d='日')

Python时间、日期、时间戳之间的转换的更多相关文章

  1. python—时间与时间戳之间的转换

    python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...

  2. python 时间与时间戳之间的转换

    https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...

  3. python——时间与时间戳之间的转换

    http://blog.csdn.net/google19890102/article/details/51355282

  4. js时间和时间戳之间如何转换(汇总)

    js时间和时间戳之间如何转换(汇总) 一.总结 一句话总结: 1.js中通过new Date()来获取时间对象, 2.这个时间对象可以通过getTime()方法获取时间戳, 3.也可以通过getYea ...

  5. 【python-时间戳】时间与时间戳之间的转换

    对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...

  6. js时间与时间戳之间的转换操作,返回天、小时、分,全家桶

    1.将时间戳转换成时间 var formatDate = function(d) {  var now = new Date(d); var year = now.getFullYear(); var ...

  7. python时间日期字符串各种

    python时间日期字符串各种 第一种 字符串转换成各种日期 time 库 # -*- coding: utf-8 -*- import time, datetime # 字符类型的时间 tss1 = ...

  8. DB2中字符、数字和日期类型之间的转换

    DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...

  9. Python数字与字符之间的转换

    Python数字与字符之间的转换 命令 意义 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 co ...

随机推荐

  1. fiddler 对https支持

    https://www.cnblogs.com/joshua317/p/8670923.html 测试可行

  2. Deep Reinforcement Learning

    Reinforcement-Learning-Introduction-Adaptive-Computation http://incompleteideas.net/book/bookdraft20 ...

  3. [转]SOA架构设计经验分享—架构、职责、数据一致性

    阅读目录: 1.背景介绍 2.SOA的架构层次 2.1.应用服务(原子服务) 2.2.组合服务 2.3.业务服务(编排服务) 3.SOA化的重构 3.1.保留服务空间,为了将来服务的组合 4.运用DD ...

  4. 力导向图Demo

    <html> <head> <meta charset="utf-8"> <title>力导向图</title> < ...

  5. 自己动手DIY macos下的绘图软件Pencil之原生菜单

    自从进入到Nodejs这个生态后,体验到了更多的可能性. Pencil是我从Linux时代就开始用的免费开源的原型/流程图软件,它之前版本是基于Firefox的XUL生态开发的,其作者从15年开始基于 ...

  6. SQLSERVER性能调优小技巧

    平时做个记录,在工作过程中针对大数据查询的一些小技巧 -----------SELECT------------- 1.必要的冗余字段,减少关联查询 2.关键查询字段必须加索引 否则百万级以上你就别想 ...

  7. php -- func_get_args

    该方法必须在某个方法内部执行才有效 返回值为索引数组,一个数组元素对应一个参数

  8. php -- 类对象调用静态方法

    以前一直以为 静态方法的调用:类名::静态方法 非静态方法的调用:类对象->非静态方法 最近研究一个类,发现一个比较奇怪的问题,用“类对象->静态方法”这种方式居然成功的调用了静态方法.很 ...

  9. 3D游戏图形引擎

    转自:http://www.cnblogs.com/live41/archive/2013/05/11/3072282.html CryEngine 3  http://www.crydev.net/ ...

  10. iOS 之 HTTPS集成实战应用

    临时想起来忘记把项目中用到的https集成整理收藏起来,以备后续不时之需.新手一般了解如下步骤即可: 1. HTTP 和 HTTPS 基本知识和学习 http://www.cnblogs.com/xi ...