一、初衷:

  很多时候,时间的存储都是时间戳格式,如果需要展示就要转化成标准格式日期。也许会需要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 日期和时间戳的互相转换的更多相关文章

  1. Python 日期和时间戳的转换

    Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...

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

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

  3. Python时间、日期、时间戳之间的转换

    一.字符串与为时间字符串之间的互相转换 方法:time模块下的strptime方法 a = "2012-11-11 23:40:00" # 字符串转换为时间字符串 import t ...

  4. python 日期、时间戳转换

    获取当前日期: from datetime import datetime IN:datetime.now() OUT:datetime(2016,10,19,6,51,21,72341) 转化为字符 ...

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

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

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

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

  7. python 日期与字符串之间的转换

    1.str转换为datetime >>> from datetime import datetime >>> cday = datetime.strptime('2 ...

  8. Python3 日期与时间戳互相转换(函数可调用)

    一.前言 在开发中,我们经常会遇到时间戳转换日期,或者日期转换为时间戳: 日期格式:2019-08-01 00:00:00 时间戳格式:1564588800 关于时间戳 Unix时间戳(Unix ti ...

  9. UTC日期转时间戳

    网上的方法用mktime来转换日期到时间戳,会被当前环境的时区影响,现在这么做,用UTC的日期转时间戳这样要转换各地的时区也简单 unsigned long utcMktime(const unsig ...

随机推荐

  1. [转]GIT PUSH Error 403的解决方法

    http://stackoverflow.com/questions/7438313/pushing-to-git-returning-error-code-403-fatal-http-reques ...

  2. Java——匿名内部类

     /* * 匿名内部类,  就是内部类的简写形式. * *  必须有前提: *  内部类必须继承或者实现一个外部类或者接口. * 匿名内部类其实就是一个子类对象. * * 格式:new 父类or接 ...

  3. weblogic管理3 - 生产模式下免密码管理配置

    admin server免密码配置 >1.  生产模式中admin root目录下是否存在security/boot.properties文件 [weblogic@11g AdminServer ...

  4. rac 11g_第二个节点重启后无法启动实例:磁盘组dismount问题

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  5. lucene文件格式待整理

    这是之前Lucene3.0生成的索引格式 a表

  6. (object sender,EventArgs e)是什么?

    object sender:发出事件的对象 EventArgs e:对象中的数据

  7. winform 调用http 通过代理出现的一系列问题

    如果遇到以上问题,请拷贝以下代码到你的桌面应用程序的app.config 文件中 <system.net> <defaultProxy enabled="false&quo ...

  8. TKinter之窗口美化 窗口大小、图标等

    设置窗口大小.设置窗口标题.设置窗口图标 效果图: 代码示例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * r ...

  9. Android 异步加载解决方案

    Android的Lazy Load主要体现在网络数据(图片)异步加载.数据库查询.复杂业务逻辑处理以及费时任务操作导致的异步处理等方面.在介绍Android开发过程中,异步处理这个常见的技术问题之前, ...

  10. es6语法重构react代码

    1.使用React.Component创建组件,需要通过在constructor中调用super()将props传递给React.Component.另外react 0.13之后props必须是不可变 ...