Python命令行日历 获取时间 格式化输出
实现简单日历
读取当前时间并显示本月日历 输出格式化
python
1#!/usr/bin/python2
2
3#It's my first Python program
4
5import time
6
7#get date 获取当前日期
8year = time.strftime('%Y', time.localtime(time.time()))
9year = (int)(year)
10month = time.strftime('%m', time.localtime(time.time()))
11day = time.strftime('%d', time.localtime(time.time()))
12day = (int)(day)
13week = time.strftime('%w', time.localtime(time.time()))
14
15week = (int)(week)
16for i in range(0, day - 1):
17 week = week - 1
18 if week == -1:
19 week = 6
20
21#judge leap 判断是否是闰年
22if year % 4 == 0 and year % 400 != 0 or year % 400 == 0 :
23 isLeap = True
24else :
25 isLeap = False
26
27#all Day这个月一共有多少天
28if month == '01' or month == '03' or month == '05' or month == '07' or month == '08' or month == '10' or month == '12' :
29 allDay = 31
30elif month == '02' and isLeap :
31 allDay = 29
32elif month == '02' and not isLeap :
33 allDay = 28
34else :
35 allDay = 30
36
37#print CAL 输出这个月的日历
38print ' ' + str(year) + ' ' + str(month)
39print ''
40print 'Sun Mon Tue Wed Thu Fri Sat'
41
42for i in range(week) : #print space 输出空白部分
43 print ' ',
44
45for i in range(1, allDay + 1) : #print everday 输出日期
46 x = str(i)
47 print '%-4s' % x,
48 week = week + 1
49 if week == 7 :
50 week = 0
51 print
52
53raw_input()如果这篇文章对你有帮助,可以请我喝杯咖啡 ☕
评论