zoukankan      html  css  js  c++  java
  • python小白-day5 time&datetime模块

    time&datetime

    ​一、time模块

     time模块提供各种操作时间的函数

    说明:一般有两种表示时间的方式:
           第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
           第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import time
    print('clock():',time.clock()) #返回处理器时间,3.3开始已废弃
    print('process_time():',time.process_time()) #返回处理器时间,3.3开始已废弃
    print('time():',time.time()) #返回当前系统时间戳
    print('ctime():',time.ctime()) #输出当前系统时间
    print('ctime(time.time()-86640):',time.ctime(time.time()-86640)) #将时间戳转为字符串格式
    print('gmtime(time.time()-86640):',time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
    print('localtime(time.time()-86640):',time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
    print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
    #time.sleep(4) #sleep
    print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
    print(time.strptime("2016-02-03","%Y-%m-%d") ) #将字符串格式转换成struct_time格式


    二、datetime模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    import time
    import datetime
    print(datetime.date.today()) #输出格式 2016-02-03
    print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-02-03 将时间戳转成日期格式
    current_time = datetime.datetime.now() #
    print(current_time) #输出2016-02-03 20:27:37.188100
    print(current_time.timetuple()) #返回struct_time格式
     
    #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
    print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
     
    str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
    new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
    print(new_date)
    new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
    print(new_date)
    new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
    print(new_date)
    new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
    print(new_date)






  • 相关阅读:
    005 ES的文档一些控制
    004 REST风格中在ES中的约定
    003 接触elasticsearch的Restful Api【快速入门】
    002 elasticsearch中的一些概念
    001 centos7下安装kibana
    000 centos7下安装elasticsearch7的单节点安装
    006 DOM节点操作与元素的创建
    006 认识BeanNameAware
    005 Spring和SpringBoot中的@Component 和@ComponentScan注解
    004 JpaRepository,CrudRepository,PagingAndSortingRepository的区别
  • 原文地址:https://www.cnblogs.com/hetan/p/5180502.html
Copyright © 2011-2022 走看看