python datetime模块
2018-06-18 00:42:13来源:未知 阅读 ()
1 import time 2 import datetime 3 4 """ 5 datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. 6 datetime模块定义了5个类,分别是 7 1.datetime.date:表示日期的类 8 2.datetime.datetime:表示日期时间的类 9 3.datetime.time:表示时间的类 10 4.datetime.timedelta:表示时间间隔,即两个时间点的间隔 11 5.datetime.tzinfo:时区的相关信息 12 """ 13 14 15 # 1、date类 16 """ 17 class date(builtins.object) 18 | date(year, month, day) --> date object 19 """ 20 # 创建datetime.date对象方法 21 print(datetime.date(2018, 1, 19)) # 3个int值 22 print(datetime.date.fromtimestamp(time.time())) # 时间戳 23 print(datetime.date(2018, 1, 20).replace(2018, 1, 19)) # 3个int值替换一个datetime.date对象 24 print(datetime.date.today()) # 今天的datetime.date对象 25 print(datetime.date.fromordinal(736713)) # 从一个日期顺序值获取,1对应0001-01-01 26 27 # datetime.date对象的运算方法 28 print(datetime.date(2018, 1, 19) + datetime.timedelta(1)) 29 print(datetime.date(2018, 1, 19) + datetime.timedelta(-1)) 30 print(datetime.date(2018, 1, 20) - datetime.date(2018, 1, 19)) 31 print(datetime.date(2018, 1, 20) - datetime.timedelta(1)) 32 print(type(datetime.date(2018, 1, 20) - datetime.date(2018, 1, 19))) # <class 'datetime.timedelta'> 33 print(datetime.date(2018, 1, 19) == datetime.date(2018, 1, 19)) 34 print(datetime.date(2018, 1, 19) >= datetime.date(2018, 1, 19)) 35 print(datetime.date(2018, 1, 19) > datetime.date(2018, 1, 19)) 36 print(datetime.date(2018, 1, 19) <= datetime.date(2018, 1, 19)) 37 print(datetime.date(2018, 1, 19) < datetime.date(2018, 1, 19)) 38 print(datetime.date(2018, 1, 19) != datetime.date(2018, 1, 19)) 39 40 # 获取datetime.date对象属性 41 print(getattr(datetime.date(2018, 1, 19), 'year')) 42 print(getattr(datetime.date(2018, 1, 19), 'month')) 43 print(getattr(datetime.date(2018, 1, 19), 'day')) 44 print(datetime.date(2018, 1, 19).year) 45 print(datetime.date(2018, 1, 19).month) 46 print(datetime.date(2018, 1, 19).day) 47 48 # 判断星期几 49 # weekday Monday == 0 ... Sunday == 6 50 print(datetime.date.weekday(datetime.date.today())) 51 # isoweekday Monday == 1 ... Sunday == 7 52 print(datetime.date.isoweekday(datetime.date.today())) 53 54 # datetime.date对象格式化 55 print(repr(datetime.date(2018, 1, 19))) # datetime.date(2018, 1, 19) 56 print(type(repr(datetime.date(2018, 1, 19)))) # <class 'str'> 57 print(str(datetime.date(2018, 1, 19))) # 2018-01-19 58 print(datetime.date.ctime(datetime.date(2018, 1, 19))) # Sat Jan 19 00:00:00 2018 59 print(datetime.date.isocalendar(datetime.date(2018, 1, 19))) # (ISO year, week number, and weekday) 60 print(datetime.date.isoformat(datetime.date(2018, 1, 19))) # 2018-01-19 61 print(datetime.date.strftime(datetime.date.today(), '%Y')) 62 print(datetime.date.timetuple(datetime.date.today())) # tm_isdst=-1 63 print(time.localtime()) # tm_isdst=0 64 65 # 其他 66 print(hash(datetime.date(2018, 1, 19))) 67 print(datetime.date.toordinal(datetime.date.today())) 68 69 70 # 2、datetime类 71 """ 72 class datetime(date) 73 | datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]) 74 | 75 | The year, month and day arguments are required. tzinfo may be None, or an 76 | instance of a tzinfo subclass. The remaining arguments may be ints. 77 """ 78 79 80 # 创建datetime.datetime对象方法 81 print(datetime.datetime(2018, 1, 19, 11, 6, 30, 1000)) 82 print(datetime.datetime.now()) 83 print(datetime.datetime.combine(datetime.date.today(), datetime.time(10, 2, 30))) 84 print(datetime.datetime.fromtimestamp(time.time())) 85 print(datetime.datetime.now().replace(2018, 1, 19, 11, 6, 30, 1000)) 86 print(datetime.datetime.strptime('2018-10-20', '%Y-%m-%d')) 87 print(datetime.datetime.utcfromtimestamp(time.time())) # 本地时间减8小时 88 print(datetime.datetime.utcnow()) 89 90 # datetime.datetime对象的运算方法 91 print(datetime.datetime.now() + datetime.timedelta(1)) 92 print(datetime.datetime.now() - datetime.timedelta(1)) 93 print(datetime.datetime.now() - datetime.datetime.now()) 94 print(datetime.datetime.now() == datetime.datetime.now()) 95 print(datetime.datetime.now() >= datetime.datetime.now()) 96 print(datetime.datetime.now() > datetime.datetime.now()) 97 print(datetime.datetime.now() <= datetime.datetime.now()) 98 print(datetime.datetime.now() < datetime.datetime.now()) 99 print(datetime.datetime.now() != datetime.datetime.now()) 100 101 # 获取datetime.datetime对象属性 102 print(getattr(datetime.datetime.now(), 'year')) 103 print(getattr(datetime.datetime.now(), 'month')) 104 print(getattr(datetime.datetime.now(), 'day')) 105 print(getattr(datetime.datetime.now(), 'hour')) 106 print(getattr(datetime.datetime.now(), 'minute')) 107 print(getattr(datetime.datetime.now(), 'second')) 108 print(getattr(datetime.datetime.now(), 'microsecond')) 109 print(getattr(datetime.datetime.now(), 'tzinfo')) 110 print(getattr(datetime.datetime.now(), 'fold')) 111 print(datetime.datetime.now().year) 112 print(datetime.datetime.now().month) 113 print(datetime.datetime.now().day) 114 print(datetime.datetime.now().hour) 115 print(datetime.datetime.now().minute) 116 print(datetime.datetime.now().second) 117 print(datetime.datetime.now().microsecond) 118 print(datetime.datetime.now().tzinfo) 119 print(datetime.datetime.now().fold) 120 121 # datetime.datetime对象格式化 122 print(repr(datetime.datetime.now())) 123 print(datetime.datetime.ctime(datetime.datetime.now())) 124 print(datetime.datetime.isoformat(datetime.datetime.now())) 125 print(datetime.datetime.timestamp(datetime.datetime.now())) 126 print(datetime.datetime.timetuple(datetime.datetime.now())) 127 print(datetime.datetime.utctimetuple(datetime.datetime.now())) 128 129 # 其他 130 print(hash(datetime.datetime.now())) 131 print(datetime.datetime.astimezone(datetime.datetime.now())) 132 print(datetime.datetime.time(datetime.datetime.now())) 133 print(datetime.datetime.timetz(datetime.datetime.now())) 134 print(datetime.datetime.tzname(datetime.datetime.now())) 135 print(datetime.datetime.date(datetime.datetime.now())) 136 print(datetime.datetime.dst(datetime.datetime.now())) 137 print(datetime.datetime.utcoffset(datetime.datetime.now())) 138 139 # 继承datetime.date对象的方法 140 # toordinal(...) 141 # fromordinal(...) 142 # isoweekday(...) 143 # weekday(...) 144 # today(...) 145 # isocalendar(...) 146 # strftime(...) 147 148 149 # 3、time类 150 """ 151 class time(builtins.object) 152 | time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object 153 | 154 | All arguments are optional. tzinfo may be None, or an instance of 155 | a tzinfo subclass. The remaining arguments may be ints. 156 """ 157 158 # 创建datetime.time对象方法 159 print(datetime.time(11, 40, 30)) 160 print(datetime.time(11, 40, 30).replace(12, 30, 20)) 161 162 # datetime.time对象的运算方法 163 print(datetime.time(11, 40, 30) == datetime.time(11, 40, 30)) 164 print(datetime.time(11, 40, 30) >= datetime.time(11, 40, 30)) 165 print(datetime.time(11, 40, 30) > datetime.time(11, 40, 30)) 166 print(datetime.time(11, 40, 30) <= datetime.time(11, 40, 30)) 167 print(datetime.time(11, 40, 30) < datetime.time(11, 40, 30)) 168 print(datetime.time(11, 40, 30) != datetime.time(11, 40, 30)) 169 170 # 获取datetime.time对象属性 171 print(getattr(datetime.time(11, 40, 30), 'hour')) 172 print(getattr(datetime.time(11, 40, 30), 'minute')) 173 print(getattr(datetime.time(11, 40, 30), 'second')) 174 print(getattr(datetime.time(11, 40, 30), 'microsecond')) 175 print(getattr(datetime.time(11, 40, 30), 'tzinfo')) 176 print(getattr(datetime.time(11, 40, 30), 'fold')) 177 print(datetime.time(11, 40, 30).hour) 178 print(datetime.time(11, 40, 30).minute) 179 print(datetime.time(11, 40, 30).second) 180 print(datetime.time(11, 40, 30).microsecond) 181 print(datetime.time(11, 40, 30).tzinfo) 182 print(datetime.time(11, 40, 30).fold) 183 184 # datetime.time对象格式化 185 print(repr(datetime.time(11, 40, 30))) 186 print(str(datetime.time(11, 40, 30))) 187 print(datetime.time.strftime(datetime.time(11, 40, 30), '%H')) 188 189 # 其他 190 print(hash(datetime.time(11, 40, 30))) 191 print(datetime.time.dst(datetime.time(11, 40, 30))) 192 print(datetime.time.isoformat(datetime.time(11, 40, 30))) 193 print(datetime.time.tzname(datetime.time(11, 40, 30))) 194 print(datetime.time.utcoffset(datetime.time(11, 40, 30))) 195 196 197 # 4、timedelta类 198 """ 199 class timedelta(builtins.object) 200 | Difference between two datetime values. 201 def __init__(self, days: float = ..., seconds: float = ..., microseconds: float = ..., 202 milliseconds: float = ..., minutes: float = ..., hours: float = ..., 203 weeks: float = ...) -> None: ... 204 """ 205 206 # 创建datetime.timedelta类对象方法 207 print(datetime.timedelta(weeks=0.5, days=1, hours=2, minutes=10, seconds=20, milliseconds=100, microseconds=10000)) 208 209 # datetime.timedelta对象的运算方法 210 print(abs(datetime.timedelta(weeks=-1, days=1, hours=2, minutes=10, seconds=20, microseconds=10000))) 211 print(datetime.timedelta(minutes=10) + datetime.timedelta(seconds=20)) 212 print(datetime.timedelta(minutes=0) != datetime.timedelta(minutes=1)) 213 print(divmod(datetime.timedelta(minutes=10), datetime.timedelta(minutes=3))) 214 print(datetime.timedelta(minutes=0) == datetime.timedelta(minutes=1)) 215 print(datetime.timedelta(minutes=10) // datetime.timedelta(minutes=3)) 216 print(datetime.timedelta(minutes=0) >= datetime.timedelta(minutes=1)) 217 print(datetime.timedelta(minutes=0) > datetime.timedelta(minutes=1)) 218 print(datetime.timedelta(minutes=0) <= datetime.timedelta(minutes=1)) 219 print(datetime.timedelta(minutes=0) < datetime.timedelta(minutes=1)) 220 print(datetime.timedelta(minutes=10) % datetime.timedelta(minutes=3)) 221 print(datetime.timedelta(minutes=10) * 3) 222 print(-datetime.timedelta(minutes=10)) 223 print(+datetime.timedelta(minutes=10)) 224 print(datetime.timedelta(minutes=10) / datetime.timedelta(minutes=3)) # float 225 226 # datetime.timedelta对象的属性 227 print(getattr(datetime.timedelta(minutes=10), 'days')) 228 print(getattr(datetime.timedelta(minutes=10), 'seconds')) 229 print(getattr(datetime.timedelta(minutes=10), 'microseconds')) 230 print(datetime.timedelta(minutes=10).days) 231 print(datetime.timedelta(minutes=10).seconds) 232 print(datetime.timedelta(minutes=10).microseconds) 233 234 # timedelta对象格式化 235 print(repr(datetime.timedelta(minutes=10))) 236 print(str(datetime.timedelta(minutes=10))) 237 238 # 其他 239 print(hash(datetime.timedelta(minutes=10))) 240 print(datetime.timedelta(minutes=10).total_seconds()) 241 print(datetime.timedelta.total_seconds(datetime.timedelta(minutes=10))) 242 243 244 # 5、timezone类 245 """ 246 class timezone(tzinfo) 247 | Fixed offset from UTC implementation of tzinfo. 248 """ 249 ti = datetime.timezone(offset=datetime.timedelta(hours=8), name='east8') # 创建时区对象 250 print(ti.tzname(datetime.datetime.now())) # east8 251 print(ti.utcoffset(datetime.datetime.now())) # 8:00:00 252 print(ti.dst(datetime.datetime.now())) # 夏令时,返回None 253 254 u = datetime.datetime(2018, 1, 19, 14, 7, tzinfo=ti) 255 t = ti.fromutc(u) 256 print(t)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash