获取当前日期和时间
from datetime import datetime
now = datetime.now() # 获取当前datetime
print(now)#2015-05-18 16:28:07.198690
print(type(now))
#<class 'datetime.datetime'>
获取指定日期和时间
from datetime import datetime
dt = datetime(2015, 4, 19, 12, 20) # 用指定日期时间创建datetime
print(dt)
#2015-04-19 12:20:00
datetime转换为timestamp
在计算机中,时间实际上是用数字表示的。我们把1970年1月1日 00:00:00 UTC+00:00时区的时刻称为epoch time,记为0(1970年以前的时间timestamp为负数),当前时间就是相对于epoch time的秒数,称为timestamp。
from datetime import datetime
dt = datetime(2015, 4, 19, 12, 20) # 用指定日期时间创建datetime
dt.timestamp() # 把datetime转换为timestamp
#1429417200.0
timestamp转换为datetime
from datetime import datetime
t = 1429417200.0
print(datetime.fromtimestamp(t))
#2015-04-19 12:20:00
str转换为datetime
from datetime import datetime
cday = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')
print(cday)
#2015-06-01 18:19:59
datetime转换为str
from datetime import datetime
now = datetime.now()
print(now.strftime('%a, %b %d %H:%M'))
#Mon, May 05 16:28
实例操作
#获取日期为星期几
import calendar
# 获取2017年11月11日是星期几(0-6代表星期一到星期日)
print(calendar.weekday(2017,11,11))#返回星期几
#datetime
#获取当前日期时间
# 获取当前日期时间(datetime类型)
import datetime
print(datetime.datetime.now())#2017-11-11 17:21:56.076882
# 获取当前日期
print(datetime.datetime.now().date())#2019-11-11
print(datetime.date.today())#2017-11-11
# 获取当前时间的字符串
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))#'2017-11-11 17:21:56'
#获取当前日期的后几天/前几天
# 获取明天
print(datetime.date.today() + datetime.timedelta(days=1))#2017-11-12
print(datetime.date.today() + datetime.timedelta(1))#2017-11-12
# 获取昨天
print(datetime.date.today() - datetime.timedelta(days=1))#2017-11-10
print(datetime.date.today() - datetime.timedelta(1))#2017-11-10
#获取本周/本月最后一天及第一天
today = datetime.date.today()
# 获取本周第一天
print(today - datetime.timedelta(today.weekday()))#2017-11-06
# 获取本周最后一天
print(today + datetime.timedelta(6-today.weekday()))#2017-11-12
# 获取本月第一天
print(datetime.date(today.year, today.month, 1))#2017-11-01
# 获取本月最后一天
import calendar
first_day_weekday, last_day_num = calendar.monthrange(today.year, today.month)
print(datetime.date(today.year, today.month, last_day_num))#2017-11-30
#获取当天最小时间/最大时间
# 获取当天最小时间
print(datetime.datetime.combine(datetime.date.today(), datetime.time.min))#2017-11-11 00:00:00
# 获取当天最大时间
print(datetime.datetime.combine(datetime.date.today(), datetime.time.max))#2017-11-11 23:59:59.999999
#获取当前时间戳time
import time
print(time.time())#1510396655.7
#获取本地时间
print(time.localtime(time.time()))
#time.struct_time(tm_year=2017, tm_mon=11, tm_mday=11, tm_hour=18, tm_min=13, tm_sec=57, tm_wday=5, tm_yday=315, tm_isdst=0)
#获取格式化时间
# 格式化成%Y-%m-%d %H:%M:%S形式
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))#2017-11-11 18:58:39
# 格式化成%a %b %d %H:%M:%S %Y形式
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))#Mon Nov 11 18:58:40 2017
# 将格式字符串转换为时间戳
format_time = time.strptime("2017-11-11 18:58:39", "%Y-%m-%d %H:%M:%S")
print(time.mktime(format_time))#1510396712.0
#各个时间格式之间的转换
#datetime <=> date
# datetime=> date
print(datetime.datetime.now().date())#2017-11-11
# date => datetime
today = datetime.date.today()
print(datetime.datetime.combine(today, datetime.time.min))#2017-11-11 00:00:00
# datetime <=> timestamp
# datetime => timestamp
import time
print(time.mktime(datetime.datetime.now().timetuple()))#1510396755.0
# timestamp => datetime
print(datetime.datetime.fromtimestamp(1510396755.0))#2017-11-11 18:39:15
#datetime <=> string日期和字符串相互转换
# datetime => string
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))#2017-11-11 18:39:15
# string => datetime
print(datetime.datetime.strptime("2017-11-11 18:39:15", "%Y-%m-%d %H:%M:%S"))#2017-11-11 18:39:15
#datetime <=> timetuple
# datetime => timetuple
import time
print(datetime.datetime.now().timetuple())
#time.struct_time(tm_year=2017, tm_mon=11, tm_mday=11, tm_hour=18, tm_min=50, tm_sec=57, tm_wday=5, tm_yday=315, tm_isdst=0)
# timetuple => datetime
time_tuple = datetime.datetime.now().timetuple()
print(datetime.datetime.fromtimestamp(time.mktime(time_tuple)))#2017-11-11 18:50:35
#===============================
import datetime
import time,calendar
print(calendar.month(2018,7))
print(datetime.date.today())#只有日期
print(datetime.datetime.now())#日期加上时间
print(datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))#格式化日期2019-08-31 22:00:47
print(datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S'))#19-08-31 22:00:47
#获取明天
print(datetime.datetime.now()+datetime.timedelta(1))
print(datetime.datetime.now()-datetime.timedelta(-1))
# 获取昨天
print(datetime.datetime.now()+datetime.timedelta(-1))
print(datetime.datetime.now()-datetime.timedelta(1))#2019-08-30 22:00:47.135102
print(datetime.datetime.now().date())#2019-08-31
print(time.time())#得到的是1567260047.1351027
print(datetime.datetime.fromtimestamp(time.time()))#2019-08-31 22:00:47.135103
print(datetime.date.fromtimestamp(time.time()))#2019-08-31
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))#格式化本地时间
import datetime
print(datetime.datetime.combine(datetime.datetime.today(),datetime.time.min))#当天最小时间
print(datetime.datetime.combine(datetime.datetime.today(),datetime.time.max))#当天最大时间
#获取本周/本月最后一天及第一天
today = datetime.date.today()
# 获取本周第一天
# print(today.weekday())
#用今天的日期减去今天是星期几 0代表星期1 星期天是6
print(today - datetime.timedelta(today.weekday()))#2017-11-06
# # 获取本周最后一天
#用今天的日期加上(一周的总天数减去今天星期几就等于还有几天是周末)
print(today + datetime.timedelta(6-today.weekday()))#2017-11-12
#
# # 获取本月第一天
print(datetime.date(today.year, today.month, 1,))#2017-11-01
#
# # 获取本月最后一天
import calendar
first_day_weekday, last_day_num = calendar.monthrange(today.year, today.month)
print(datetime.date(today.year, today.month, last_day_num))#2017-11-30