天天看點

python 月曆_Python 日期和時間

time.time()擷取目前時間戳

例如:

#!/usr/bin/python

#-*- coding: UTF-8 -*-

importtime; # 引入time子產品

ticks=time.time()

print"目前時間戳為:", ticks

結果:

目前時間戳為: 1491750320.74

時間元組

python 月曆_Python 日期和時間

比如擷取目前時間:

#!/usr/bin/python#-*- coding: UTF-8 -*-

importtime

localtime=time.localtime(time.time())print "本地時間為 :", localtime

擷取格式化的時間

localtime =time.asctime( time.localtime(time.time()) )print "本地時間為 :", localtime

結果:

本地時間為 : Sun Apr 09 23:12:15 2017

格式化日期

格式有點類似java的日期格式化

importtime#格式化成2016-03-20 11:45:39形式

print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())#格式化成Sat Mar 28 22:24:24 2016形式

print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())#将格式字元串轉換為時間戳

a = "Sat Mar 28 22:24:24 2016"

print time.mktime(time.strptime(a, "%a %b %d %H:%M:%S %Y"))

結果:

2017-04-09 23:13:50Sun Apr 09 23:13:50 2017

1459175064.0

日期格式化符号

python 月曆_Python 日期和時間

擷取某月月曆

直接以月曆的形式顯示某月的資訊

2017年4月份的月曆:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import calendar

cal = calendar.month(2017, 4)

print "以下輸出2017年4月份的月曆:"

print cal;

顯示結果:

以下輸出2017年4月份的月曆:

April 2017

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

Time 子產品

python 月曆_Python 日期和時間
python 月曆_Python 日期和時間

月曆(Calendar)子產品

python 月曆_Python 日期和時間

原文位址:http://www.cnblogs.com/androidsuperman/p/6687056.html