Shell Python 日期和时间戳的互相转换
一、初衷:
很多时候,时间的存储都是时间戳格式,如果需要展示就要转化成标准格式日期。也许会需要date和timestamp互转。
二、方法:
1、Shell下对date和timestamp的互转,是通过date函数
date --> timestamp : $date -d '2015-01-31 23:20:20' +%s
结果 1422717620
timestamp --> date : $date -d '1970-01-01 1422717620 sec utc'
结果 Sat Jan 31 23:20:20 CST 2015
2、Python 通过time模块转换
date --> timestamp
a = "2013-10-10 23:40:00"
#将其转换为时间数组
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
#转换为时间戳:
timeStamp = int(time.mktime(timeArray))
timeStamp == 1381419600
timestamp --> date
利用localtime()转换为时间数组,然后格式化为需要的格式,如:
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
otherStyletime == "2013-10-10 23:40:00"
3、Shell Python获取当前时间日期:
Shell:now = `date`
Python: now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
另附:
Shell date函数使用方法:man date
Python time模块:https://docs.python.org/2/library/time.html
Shell Python 日期和时间戳的互相转换的更多相关文章
- Python 日期和时间戳的转换
Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- Python时间、日期、时间戳之间的转换
一.字符串与为时间字符串之间的互相转换 方法:time模块下的strptime方法 a = "2012-11-11 23:40:00" # 字符串转换为时间字符串 import t ...
- python 日期、时间戳转换
获取当前日期: from datetime import datetime IN:datetime.now() OUT:datetime(2016,10,19,6,51,21,72341) 转化为字符 ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- python——时间与时间戳之间的转换
http://blog.csdn.net/google19890102/article/details/51355282
- python 日期与字符串之间的转换
1.str转换为datetime >>> from datetime import datetime >>> cday = datetime.strptime('2 ...
- Python3 日期与时间戳互相转换(函数可调用)
一.前言 在开发中,我们经常会遇到时间戳转换日期,或者日期转换为时间戳: 日期格式:2019-08-01 00:00:00 时间戳格式:1564588800 关于时间戳 Unix时间戳(Unix ti ...
- UTC日期转时间戳
网上的方法用mktime来转换日期到时间戳,会被当前环境的时区影响,现在这么做,用UTC的日期转时间戳这样要转换各地的时区也简单 unsigned long utcMktime(const unsig ...
随机推荐
- Vue.js相关知识4-路由
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- objective-c new关键字
xxx *a = [xxx new] 等价于 xxx *a = [[xxx alloc]init] ,但如果类的构造函数带参数就不能使用new了. 练习了下<Objective-C 基础教程&g ...
- C++@命名空间(转)
转自http://hi.baidu.com/rainysky_2006/blog/item/a490e01fc3de7964f724e4d1.html 本讲基本要求 * 掌握:命名空间的作用及定义:如 ...
- 采用PHP函数uniqid生成一个唯一的ID
http://www.daimajiayuan.com/sitejs-17815-1.html
- Attention and Augmented Recurrent Neural Networks
Attention and Augmented Recurrent Neural Networks CHRIS OLAHGoogle Brain SHAN CARTERGoogle Brain Sep ...
- ps互补色
色彩中的互补色有红色与绿色互补,蓝色与橙色互补,紫色与黄色互补.在光学中指两种色光以适当的比例混合而能产生白光时,则这两种颜色就称为“互为补色”. 互补色是相对的混合的白色 互补色:在色环中某种颜色的 ...
- POI读取excel
HSSF是Horrible Spread Sheet Format的缩写 读取2007版本前 XSSF是XML Spread Sheet Format的缩写 读取2007版本后(包含2007)
- Unity3D深入浅出 - 新版粒子系统 (Shuriken) - Tonge
Shuriken粒子系统是继Unity3.5版本之后推出的新版粒子系统,它采用了模块化管理,个性化的粒子模块配合粒子曲线编辑器使用户更容易创作出各种兵分复杂的粒子效果. 创建一个粒子系统的方式有两种: ...
- Python的numpy库下的几个小函数的用法
numpy库是Python进行数据分析和矩阵运算的一个非常重要的库,可以说numpy让Python有了matlab的味道 本文主要介绍几个numpy库下的小函数. 1.mat函数 mat函数可以将目标 ...
- [oracle] ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener
安装好Oracle数据库后: 执行 dbstart和dbshut会提示: [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, un ...