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

  方法: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. AspectF写法

    AspectF.Define .ProgressBar(caption) .Do(() => { if (!SpecialMenuClick(midForm, tag)) { DockBarSh ...

  2. Cocos 编译android-studio

    3.15.1 之前: http://www.jianshu.com/p/ac2bac4734b8 http://www.jianshu.com/p/3d0cc85460d1 在工程项目下 运行 coc ...

  3. SQL递归方式实现省市区县级别查询

    数据库脚本 CREATE TABLE [dbo].[Std_Area]( [Id] [int] NOT NULL, [Name] [nvarchar](50) NULL, [ParentId] [in ...

  4. Go使用详解

    1.什么是Go keep it simple stupid的编程语言 2.安装 以Ubuntu为例 # 下载安装包 wget https://storage.googleapis.com/golang ...

  5. vs code 设置问题

    现已取消 .vue 文件与 HTML 的默认关联,需要手动配置.vue 文件里不能使用div + Tab 键快速生成 html 代码   "emmet.syntaxProfiles" ...

  6. linux shell命令之wc/split及特殊字符

    [时间:2018-07] [状态:Open] [关键词:linux, wc, split, 通配符,转义符,linux命令] 0 引言 整理这篇文章的目的不是为了什么学习,仅仅是为了强化下记忆,以便下 ...

  7. Python生成随机字符串

    利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...

  8. 蜻蜓特派员 Windows XP SP3 纯净终结版

    蜻蜓特派员Windows XP SP3 纯净安装版 终结版,系统纯净无广告.无插件,网卡等驱动和运行库齐全,安全更新补丁全网最新!微软停止了 Windows XP 的支持之后还是偶尔为 WinXP 提 ...

  9. gsoap使用总结

    WebService.soap.gsoap基本概念 WebService服务基本概念:就是一个应用程序,它向外界暴露出一个可以通过web进行调用的API,是分布式的服务组件.本质上就是要以标准的形式实 ...

  10. linux java 安装

    对于java的开发,有openJDK 和 orcale jdk两种,大多数的linux 系统都会内置openjdk的安装包,但是大多数java项目的开发都是基于orcale jdk的,所以安装orca ...