002---time & datetime

time module

Time-related modules

  • classification
    • Timestamp
    • Time string
    • Time Ganso
  • definition
    • UTC: Greenwich Mean Time, Universal Time, Chinese (UTC + 8)
    • Timestamp: 1970-01-010: 0: 0 counted in seconds, returns a float
    • Ganso: struct_time Ganso total of nine elements.

Three forms:

  1. Timestamp: time.time () time difference, time
  2. Humans can read time, formatted time .2019-6-28 12:00
  3. Structured Time: python language, the interpreter uses

3's transformation

import time

print(time.time()) # 1561707352.6777651

# 格式化时间:
# 字符串类型
print(time.strftime("%Y-%m-%d %H:%M:%S"))  # 2019-06-28 15:36:32
print(time.strftime("%y-%m-%d %H:%M:%S  %A")) # 19-06-28 15:36:32  Friday
ret = time.strftime("%Y{}%m{}%d{} %H:%M:%S")
print(ret) #  2019{}06{}28{} 15:36:32
print(ret.format('年','月','日')) # 2019年06月28日 15:36:32

print(time.localtime()) # time.struct_time(tm_year=2019, tm_mon=6, tm_mday=28, tm_hour=15, tm_min=36, tm_sec=32, tm_wday=4, tm_yday=179, tm_isdst=0)
e(st)  # 转成时间戳
print(timestamp)

Time conversion column title

# 时间戳转化成格式化时间
import time
st = time.localtime(8340768 可以直接输时间戳) # 转成结构化时间
print(st)#命名元祖
ft = time.strftime("%Y-%m-%d %H:%M:%S") # 转成格式化时间
print(ft)

# 格式化时间转化成时间戳
import time
date = input("请输入X年X月X日:").strip()
#2019-5-9
st = time.strptime(date, "%Y-%m-%d")  # 转成结构化时间 命名元祖

timestamp = time.mktime(st)  # 转成时间戳
print(timestamp)

列题
import time
date = input("请输入X年X月X日:").strip()
#2019-5-9
st = time.strptime(date, "%Y-%m-%d")  # 转成结构化时间 命名元祖
print(st.tm_yday)#多少天

datetime module

Date time module for jumping

import datetime
# from datetime import datetime
time_now = datetime.datetime.now()
print(str(time_now),type(time_now)) # 2019-06-28 15:46:50.525403 <class 'datetime.datetime'>

print(datetime.datetime.now() + datetime.timedelta(weeks=3)) # 2019-07-19 15:47:11.392616
print(datetime.datetime.now() + datetime.timedelta(weeks=-4))  # 2019-05-31 15:47:11.400563
print(datetime.datetime.now() + datetime.timedelta(days=-15))  # 2019-06-13 15:47:11.400563

current_time = datetime.datetime.now()
print(current_time.replace(year=1979))  # 1979-06-28 15:48:20.118009
print(current_time.replace(year=1989,month=4,day=25)) # 1989-04-25 15:48:20.118009

print(datetime.date.fromtimestamp(1332543543)) # 2012-03-24

Guess you like

Origin www.cnblogs.com/saoqiang/p/12177065.html