008 python语法_类 time

 

 

'''
时间:2018/10/28
目录: 
  一: 概述
        1 help查看
        2 文件查看
        3 类型查看
  二: 使用
        1 获取时间戳
        2 获取当前时间
        3 获取日历
        4 获取日期
        5 获取日期 - 精确毫秒
'''

 

一: 概述
  1 help查看

# coding:utf-8
import time

help(time)
D:\ProgramTools\Python\python.exe "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/StudyJson/StudyJson/Study001.py"
Help on built-in module time:

NAME
    time - This module provides various functions to manipulate time values.

DESCRIPTION
    There are two standard representations of time.  One is the number
    of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
    or a floating point number (to represent fractions of seconds).
    The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
    The actual value can be retrieved by calling gmtime(0).
    

 

  2 文件查看

# encoding: utf-8
# module time
# from (built-in)
# by generator 1.138
"""
This module provides various functions to manipulate time values.

There are two standard representations of time.  One is the number
of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
or a floating point number (to represent fractions of seconds).
The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
The actual value can be retrieved by calling gmtime(0).

 

  3 类型查看

# coding:utf-8
import time

print(type(time))
<class 'module'>

Process finished with exit code 0

 

二: 使用
  1 获取时间戳

# coding:utf-8
import time

ticks = time.time()        # 获取当前时间戳
print(ticks)
print(type(ticks))
1540734821.25581
<class 'float'>

 

  2 获取当前时间

# coding:utf-8
import time

localtime = time.localtime(time.time())        # 获取当前时间戳
print(localtime)
print(type(localtime))
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=28, tm_hour=21, tm_min=54, tm_sec=20, tm_wday=6, tm_yday=301, tm_isdst=0)
<class 'time.struct_time'>

 

  3 获取日历

# coding:utf-8
import calendar

cal = calendar.month(2018, 10)  # 获取某月日历
print(cal)
print(type(cal))
    October 2018
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

<class 'str'>

 

  4 获取日期

# coding:utf-8
import time

strTime = time.strftime("%Y/%m/%d %H:%M:%S")    # 格式化日期
print(strTime)
print(type(strTime))
2018/10/28 21:58:55
<class 'str'>

 

  5 获取日期 - 精确毫秒

# coding:utf-8
import datetime

strTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %f')    # 获取日期 - 精确毫秒
print(strTime)
print(type(strTime))
2018-10-28 22:01:35 751949
<class 'str'>

 

posted @ 2018-10-28 21:53  火焰马  阅读(260)  评论(0编辑  收藏  举报