Python基于datetime或time模块分别获取当前时间戳的方法实例

作者:Together_CZ 时间:2021-01-19 22:53:18 

python的时间模块生成时间戳的方法是非常简单的,因为最近频繁用到了时间戳功能,这里简单总结了一下日常使用最为频繁的两个时间模块各自生成当前时间戳的方法,很简单,具体如下:


now_time=str(datetime.datetime.now().strftime('%Y%m%d'))
nowTime=str(time.strftime('%Y%m%d',time.localtime(time.time())))
print 'now_time:',now_time
print 'nowTime:',nowTime

结果如下:

now_time: 20181226
nowTime: 20181226

上面是生成年月日的时间戳,如果要精确到秒级可以使用下面的方法:


now_time=str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
nowTime=str(time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())))
print 'now_time:',now_time
print 'nowTime:',nowTime

结果如下:

now_time: 20181226091741
nowTime: 20181226091741

当然想使用不同的分隔符号还可以有下面的形式:


now_time=str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
nowTime=str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
print 'now_time:',now_time
print 'nowTime:',nowTime

now_time=str(datetime.datetime.now().strftime('%Y/%m/%d/%H:%M:%S'))
nowTime=str(time.strftime('%Y/%m/%d/%H:%M:%S',time.localtime(time.time())))
print 'now_time:',now_time
print 'nowTime:',nowTime

结果如下:

now_time: 2018-12-26 09:18:58
nowTime: 2018-12-26 09:18:58
now_time: 2018/12/26/09:18:58
nowTime: 2018/12/26/09:18:58

来源:https://blog.csdn.net/Together_CZ/article/details/85256753

标签:python,datetime模块,time模块,时间戳
0
投稿

猜你喜欢

  • Python3 适合初学者学习的银行账户登录系统实例

    2021-06-16 05:09:22
  • Python脚本调试工具安装过程

    2021-08-02 23:10:53
  • pandas DataFrame 交集并集补集的实现

    2023-05-02 12:03:27
  • EXECUTE IMMEDIATE用法小结

    2009-09-26 18:32:00
  • Django自定义分页效果

    2023-12-06 00:09:04
  • python向已存在的excel中新增表,不覆盖原数据的实例

    2022-04-26 15:16:51
  • python使用hdfs3模块对hdfs进行操作详解

    2023-10-30 07:12:03
  • Scrapy框架爬取Boss直聘网Python职位信息的源码

    2022-06-30 21:02:47
  • python3字符串输出常见面试题总结

    2021-01-13 08:39:23
  • Python爬虫包BeautifulSoup简介与安装(一)

    2021-07-25 05:06:10
  • Python3中多线程编程的队列运作示例

    2022-06-15 23:27:49
  • Python字符串的索引与切片

    2021-08-17 23:46:00
  • Python中常用的内置函数

    2023-01-06 08:15:30
  • Python正则简单实例分析

    2023-03-10 16:49:59
  • Python读取Excel数据实现批量生成PPT

    2021-03-05 23:57:35
  • Python import与from import使用及区别介绍

    2022-05-17 12:09:19
  • Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决

    2023-04-21 05:52:45
  • python实现过滤敏感词

    2021-02-26 04:23:17
  • python re模块findall()函数实例解析

    2022-07-07 13:38:40
  • 详解Python中的循环语句的用法

    2023-10-26 08:37:35
  • asp之家 网络编程 m.aspxhome.com