字符串转时间

import time# 字符类型的时间
tss1 = '2013-10-10 23:40:00'
# 转为时间数组
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
print(timeArray)
# timeArray可以调用tm_year等
print(timeArray.tm_year)  # 2013

时间转时间戳

import timelocaltime = time.localtime(time.time())
print("本地时间为:", localtime)
timeStamp = int(time.mktime(localtime))
print(timeStamp)  # 1381419600

更改字符串类型日期的显示格式

import timetss2 = "2013-10-10 23:40:00"
# 转为数组
timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
# 转为其它显示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(otherStyleTime)  # 2013/10/10 23:40:00tss3 = "2013/10/10 23:40:00"
timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)  # 2013-10-10 23:40:00

时间戳转换为指定格式的日期(UTC时间)

# 使用time
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print(otherStyleTime)   # 2013--10--10 23:40:00
# 使用datetime
timeStamp = 1381419600
dateArray = datetime.datetime.fromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print(otherStyleTime)   # 2013--10--10 23:40:00
# 使用datetime,指定utc时间,相差8小时
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print(otherStyleTime)   # 2013--10--10 15:40:00

获取当前时间并且用指定格式显示

import time 

import time# time获取当前时间戳
now = int(time.time())  # 1533952277
timeArray = time.localtime(now)
print(timeArray)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print(otherStyleTime)

结果:

time.struct_time(tm_year=2020, tm_mon=12, tm_mday=3, tm_hour=11, tm_min=57, tm_sec=53, tm_wday=3, tm_yday=338, tm_isdst=0)
2020--12--03 11:57:53

import datetime 

import datetime# datetime获取当前时间,数组格式
now = datetime.datetime.now()
print(now)
otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
print(otherStyleTime)

结果:

2020-12-03 14:22:39.017879
2020--12--03 14:22:39

Python 字符串、时间、日期、时间戳的相互转换(时间戳转换)相关推荐

  1. python字符串转date_详解python 字符串和日期之间转换 StringAndDate

    python 字符串和日期之间转换 StringAndDate 这里给出实现代码,直接可以使用.大家可以看下. 实例代码: ''''' Created on 2013-7-25 @author: Ad ...

  2. 2021年大数据Hive(五):Hive的内置函数(数学、字符串、日期、条件、转换、行转列)

    全网最详细的Hive文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 系列历史文章 前言 Hive的内置函数 一.数学函数 1. 取整函数: round ...

  3. Python 创建时间日期datetime对象

    datetime 模块提供用于处理日期和时间的类.在支持日期时间数学运算的同时,实现的关注点更着重于如何能够更有效地解析其属性用于格式化输出和数据操作.本文主要介绍Python 创建时间日期datet ...

  4. Python字符串与字节串的相互转换

    Python字符串与字节串的相互转换 字符串转字节串 字节串转字符串 在使用Python做网络编程时,难免会遇到字节串与字符串相互转换,在此记录下几种相互转换的方法: 字符串转字节串 方法一: 方法二 ...

  5. python中时间日期相减并转化为秒

    python在时间日期转化时候怎样相减转化 秒 ,刚有人咨询我这些问题,写了一些代码 希望对你有点用 from datetime import datetime now_time = datetime ...

  6. Python - 字符串转日期时间,格式的处理以及时间加减计算

    1,字符串转日期 import datetime strTime = '2022-06-11 11:03' strTime = datetime.datetime.strptime(strTime,& ...

  7. python字符串转日期_python 日期、时间、字符串相互转换

    python 日期.时间.字符串相互转换 在python中,日期类型date和日期时间类型dateTime是不能比较的. (1)如果要比较,可以将dateTime转换为date,date不能直接转换为 ...

  8. python字符串转为日期datetime、date时间元组

    python 字符串转化为日期格式 datetime.date.时间元组 1. 字符串 --> 日期格式:datetime.datetime(2020, 9, 23, 16, 47, 8) im ...

  9. [转载] 整理总结 python 中时间日期类数据处理与类型转换(含 pandas)

    参考链接: Python中的时间函数 2(日期操作) 我自学 python 编程并付诸实战,迄今三个月. pandas可能是我最高频使用的库,基于它的易学.实用,我也非常建议朋友们去尝试它.--尤其当 ...

  10. python对时间日期做格式化

    From: http://www.cnblogs.com/65702708/archive/2011/04/17/2018936.html Python格式化日期时间的函数为datetime.date ...

最新文章

  1. linux message日志只有4k,命令长期运行 常用技巧 Linux 服务器 · 404k的前后端日志...
  2. java 虚拟打印机_Java 通过物理、虚拟打印机打印Word文档
  3. navicat 连接oracle
  4. extjs 验证消息不显示
  5. 单调栈:leetcode 84. 柱状图中最大的矩形/85最大矩形
  6. 拓端tecdat|R语言中自编基尼系数的CART回归决策树的实现
  7. 如何使用segy数据绘制地震剖面
  8. “黎明”号新任务继续“锁定”谷神星
  9. 【树莓派】搭建OpenWrt软路由,并作为旁路由的配置与应用方法
  10. winform 鼠标拖动移动图片位置
  11. Oracle兵器谱上古神器之-KFED
  12. 率土之滨鸿蒙团,【率土之滨】无需“垒实”也能鏖战全场!群吕布混编弓解析...
  13. KRPANO资源分析工具下载720YUN全景图
  14. 腾讯视频VIP周卡深圳地区免费领!附非深圳免费领腾讯视频会员攻略
  15. 对不起我爱你在线观看(完整版)
  16. SQL Server数据库开发(3.SQL高级查询)
  17. 沙河之痛——河南鲁山县沙河非法采砂遥感影像分析
  18. 深山红叶安装到U盘(HDD方式)的另法
  19. window tomcat 启动后 点击cmd窗口 快速编辑模式导致项目宕机问题
  20. alfresco6.1(源码版) + onlyoffice安装部署

热门文章

  1. 这个工具可以快速查看文章引用、获取全文、研究者状态
  2. Ngs File Type Transfer
  3. Mac备忘录笔记教学——强大的内置笔记软件
  4. 洛谷 深基 第1部分 语言入门 第4章 循环结构程序设计(2022.02.14)
  5. 1.4编程基础之逻辑表达式与条件分支 07 收集瓶盖赢大奖
  6. 白鹭引擎写入文字图层方法实例
  7. thinkphp5.0l路由冲突原因及解决方法
  8. 两个构件的重合点_GTJ2018软件中如何合并两个工程?
  9. 文件管理软件 云服务器,使用KDE Plasma文件管理器(Dolphin)连接到Linux上的服务器...
  10. javascript变量提升/函数提升