Python日常-格式化日期与时间戳转换

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time

if __name__ == '__main__':
    # 获取当前时间戳
    now_time_stamp = time.time()
    print now_time_stamp

    # 将时间戳转换为时间数组
    now_localtime = time.localtime(now_time_stamp)
    print now_localtime

    # 将时间数组格式化为各种格式化的时间字符串
    now_format_time = time.strftime('%Y-%m-%d %H:%M:%S',now_localtime)
    print now_format_time
    now_format_date = time.strftime('%Y-%m-%d',now_localtime)
    print now_format_date
    now_format_clock = time.strftime('%H:%M:%S',now_localtime)
    print now_format_clock

    print "=======================================================\n"

    # 设置一个时间字符串
    my_date = "2015-01-01 12:00:00"
    print my_date

    # 将时间字符串转换为时间数组
    my_date_array = time.strptime(my_date,"%Y-%m-%d %H:%M:%S")
    print my_date_array

    # 将时间数组转换为时间戳
    my_date_stamp = time.mktime(my_date_array)
    print my_date_stamp
1486197824.01
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=16, tm_min=43, tm_sec=44, tm_wday=5, tm_yday=35, tm_isdst=0)
2017-02-04 16:43:44
2017-02-04
16:43:44
=======================================================

2015-01-01 12:00:00
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=12, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1)
1420084800.0
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值