zoukankan      html  css  js  c++  java
  • python日期与时间

    1.介绍

      Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。

      时间间隔是以秒为单位的浮点小数。

      每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。

    一:获取时间戳

    1.程序

    1 # -*- coding: UTF-8 -*-
    2 
    3 import time;  # 引入time模块
    4 
    5 ticks = time.time()
    6 print "当前时间戳为:", ticks

    二:获取当前时间

    1.时间元祖

      Python函数用一个元组装起来的9组数字处理时间

      

    2.程序

      从返回浮点数的时间戳方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。

    1 # -*- coding: UTF-8 -*-
    2 import time
    3 
    4 localtime = time.localtime(time.time())
    5 print "本地时间为 :", localtime

    3.效果

      

    三:获取格式化的时间      

    1.程序

    1 # -*- coding: UTF-8 -*-
    2 import time
    3 
    4 localtime = time.asctime( time.localtime(time.time()) )
    5 print "本地时间为 :", localtime

    2.效果

      

    --------=====================================================================================日期=====================

    四:格式化日期

    1.介绍

      我们可以使用 time 模块的 strftime 方法来格式化日期

    2.格式化符号

      

     3.程序

     1 # -*- coding: UTF-8 -*-
     2 import time
     3 
     4 # 格式化成2016-03-20 11:45:39形式
     5 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
     6 
     7 # 格式化成Sat Mar 28 22:24:24 2016形式
     8 print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
     9 
    10 # 将格式字符串转换为时间戳
    11 a = "Sat Mar 28 22:24:24 2016"
    12 print time.mktime(time.strptime(a, "%a %b %d %H:%M:%S %Y"))
    13 
    14 print time.localtime()

    4.效果

      

    五:获取日历

    1.介绍

      Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历

    2.程序

    1 # -*- coding: UTF-8 -*-
    2 import calendar
    3 
    4 cal = calendar.month(2016, 1)
    5 print "以下输出2016年1月份的日历:"
    6 print cal;

    3.效果

      

    六:Time模块

    1.函数

      

    七:Calendar模块

    1.函数

      

    八:相关模块

  • 相关阅读:
    hdu 1518 Square 深搜,,,,花样剪枝啊!!!
    3D拾取技术
    cocos2dx 使用spine制作骨骼动画
    poj3080Blue Jeans
    G4Studio+extjs+highcharts 下在ext4j的panel中放入hightCharts图表
    快的打车架构实践
    电源管理ACPI、及APM、GNU/Linux系统下的相应命令使用
    ------银行系统------
    ARM+llinux系统移植3G拨号上网收发短信(一)【转】
    在ARM-linux上实现4G模块PPP拨号上网【转】
  • 原文地址:https://www.cnblogs.com/juncaoit/p/8525610.html
Copyright © 2011-2022 走看看