python杂记-6(time&datetime模块)

本文详细介绍了使用Python进行时间与日期的基本操作,包括获取当前时间、日期格式转换、时间戳处理以及时间间隔的加减等实用技巧。文章还提供了丰富的代码示例帮助读者更好地理解和掌握这些操作。
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time

print(time.clock())##返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.process_time())
print(time.altzone)#返回与utc时间的时间差,以秒计算\
print(time.asctime())#返回时间格式“Thu Nov 3 10:04:39 2016”
print(time.localtime())#返回本地时间的struct time 对象格式
print(time.gmtime(time.time()-800000))#返回utc时间的struc时间对象格式

print(time.asctime(time.localtime()))#返回时间格式“Thu Nov 3 10:07:36 2016”
print(time.ctime())#返回Thu Nov 3 10:08:41 2016

#日期字符串转成 时间戳
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d")#将日期字符串转成 struct时间对象格式
print(string_2_struct)
#
struct_2_stamp = time.mktime(string_2_struct)#将struct时间对象转成时间戳
print(struct_2_stamp)

#将时间戳转为字符串格式
print(time.gmtime(time.time()-86640))#将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))#将utc struct_time格式转成指定的字符串格式

#时间加减
import datetime

print(datetime.datetime.now())#返回 2016-11-03 10:51:04.385070
print(datetime.date.fromtimestamp(time.time()))#时间戳直接转成日期格式 2016-11-03
print(datetime.datetime.now()+datetime.timedelta(3))# 当前时间+3天
print(datetime.datetime.now()+datetime.timedelta(-3))# 当前时间-3天
print(datetime.datetime.now()+datetime.timedelta(hours=3))#当前时间+3小时
print(datetime.datetime.now()+datetime.timedelta(minutes=3))#当前时间+3分钟

#
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换


DirectiveMeaningNotes
%aLocale’s abbreviated weekday name. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month name. 
%BLocale’s full month name. 
%cLocale’s appropriate date and time representation. 
%dDay of the month as a decimal number [01,31]. 
%HHour (24-hour clock) as a decimal number [00,23]. 
%IHour (12-hour clock) as a decimal number [01,12]. 
%jDay of the year as a decimal number [001,366]. 
%mMonth as a decimal number [01,12]. 
%MMinute as a decimal number [00,59]. 
%pLocale’s equivalent of either AM or PM.(1)
%SSecond as a decimal number [00,61].(2)
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(3)
%wWeekday as a decimal number [0(Sunday),6]. 
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(3)
%xLocale’s appropriate date representation. 
%XLocale’s appropriate time representation. 
%yYear without century as a decimal number [00,99]. 
%YYear with century as a decimal number. 
%zTime zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. 
%ZTime zone name (no characters if no time zone exists). 
%%A literal '%' character.

转载于:https://www.cnblogs.com/LS-blog/p/6025773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值