Skip to content

Instantly share code, notes, and snippets.

@ranlix
Created March 5, 2014 03:05
Show Gist options
  • Save ranlix/9360413 to your computer and use it in GitHub Desktop.
Save ranlix/9360413 to your computer and use it in GitHub Desktop.
datetime 和calendar 模块里没有直接可以查询到某一个月份总天数等,就写了一个YearMonthDay的class,方便以后调用
# -*- coding:utf-8 -*-
#!/usr/bin/python
import re
import math
import time
import os
import csv
import sys
import datetime
import calendar
weekdayList = ["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Satursday", "Sunday"]
tempList = range(1, 8)
weekdayDic = dict(zip(tempList, weekdayList))
# print weekdayDic
class YearMonthDay(object):
def __init__(self, someday):
date_patt = "^(\d{4})(\-|\.|\/)?(\d){1,2}(\-|\.|\/)?(\d){1,2}"
result = re.search(date_patt, someday)
self.year = int(result.group(1))
self.month = int(result.group(3))
self.day = int(result.group(5))
def month_days(self):
"""
Return total days of current month(this instance)
"""
x = calendar.month(self.year, self.month)
return int(x.split(" ")[-1].split("\n")[0])
if __name__ == '__main__':
# anyday = raw_input("请输入需要查询的日期,例如'2014.1.1':")
anyday = "2014.3.3"
# print month_days(2020, 2)
thatday = YearMonthDay(anyday)
# print thatday.month_days()
print "This day is %d(year)%d(month)%d(day)" % \
(thatday.year, thatday.month, thatday.day)
print "And it's %s" % (weekdayDic[datetime.datetime.weekday(datetime.date(
thatday.year, thatday.month, thatday.day)) + 1])
@ranlix
Copy link
Author

ranlix commented Mar 5, 2014

Run result:
{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Satursday', 7: 'Sunday'}
This day is 2014(year)3(month)3(day)
And it's Monday
[Finished in 1.2s]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment