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

  方法: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. mac关闭占用某个端口的进程

    在启动项目的时候有时候会提示端口被占用,但是怎么都找不到那个关闭进程的地方,可以直接通过命令行关闭这个进程: 比如要关闭:8000端口的进程: 1. 查找端口进程: lsof -i: 会把所有的占用8 ...

  2. 【基础篇】elasticsearch之索引模板Template[转]

    一,模板简述:template大致分成setting和mappings两部分:索引可使用预定义的模板进行创建,这个模板称作Index templates.模板设置包括settings和mappings ...

  3. Python类中的装饰器在当前类中的声明与调用

    [本文出自天外归云的博客园] 我的Python环境:3.7 在Python类里声明一个装饰器,并在这个类里调用这个装饰器.代码如下: class Test(): xx = False def __in ...

  4. find ctime 加减n时间范围

    看下atime的时间解释:-atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour ...

  5. acl && prefix list

    acl number 2001                                         设置acl的编号rule 0 permit source 1.1.1.0 0.0.0.2 ...

  6. The client and server cannot communicate, because they do not possess a common algorithm

    The client and server cannot communicate, because they do not possess a common algorithm This was re ...

  7. CentOS安装和配置Rsync进行文件同步

    Liunx系统实现文件同步不需要搭建FTP这类的工具,只需要按照Rsync配置下文件就可以. 本文以Centos7.0为例. 1. 首先关闭SELINUX(不关闭无法同步,权限太高了) vi /etc ...

  8. Kafka consumer poll(long)与poll(Duration)的区别

    最近在StackOverflow碰到的一个问题,即在consumer.poll之后assignment()返回为空的问题,如下面这段代码所示: consumer.subscribe(Arrays.as ...

  9. redis数据操作笔记

    redis是key-value的数据结构,每条数据都是一个键值对键的类型是字符串 注意:键不能重复,值的类型分为五种:字符串string 哈希hash 列表list 集合set 有序集合zset 一. ...

  10. C#俄罗斯方块小游戏程序设计与简单实现

    C#俄罗斯方块小游戏程序设计与简单实现 相信90后或者80后都玩过这款小游戏,一直想干一票,琢磨一下,但又不太懂,于是网上搜集修改就有了以下效果!bug较多,多多包涵! 1.效果展示 2.实现方法 参 ...