如何在Python中给出一个date来获得星期几?
我想找出以下内容:给定一个date( datetime
对象),一周的相应的一天是什么。
例如星期天是星期一的第一天,第二天……等等
然后,如果input是像今天的date。
例
>>> today = datetime.datetime(2017, 10, 20) >>> today.get_weekday() # what I look for
输出可能是6
(从星期五开始)
使用weekday()
( docs ):
>>> import datetime >>> datetime.datetime.today() datetime.datetime(2012, 3, 23, 23, 24, 55, 173504) >>> datetime.datetime.today().weekday() 4
从文档:
将星期几作为整数返回,星期一为0,星期日为6。
如果你想用英文写date:
>>> from datetime import date >>> import calendar >>> my_date = date.today() >>> calendar.day_name[my_date.weekday()] 'Wednesday'
使用date.weekday()
或date.isoweekday()
。
我解决了这个codechef 问题 。
import datetime dt = '21/03/2012' day, month, year = (int(x) for x in dt.split('/')) ans = datetime.date(year, month, day) print ans.strftime("%A")
1700/1/1之后的date没有import的解决scheme
def weekDay(year, month, day): offset = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] afterFeb = 1 if month > 2: afterFeb = 0 aux = year - 1700 - afterFeb # dayOfWeek for 1700/1/1 = 5, Friday dayOfWeek = 5 # partial sum of days betweem current date and 1700/1/1 dayOfWeek += (aux + afterFeb) * 365 # leap year correction dayOfWeek += aux / 4 - aux / 100 + (aux + 100) / 400 # sum monthly and day offsets dayOfWeek += offset[month - 1] + (day - 1) dayOfWeek %= 7 return dayOfWeek, week[dayOfWeek] print weekDay(2013, 6, 15) == (6, 'Saturday') print weekDay(1969, 7, 20) == (0, 'Sunday') print weekDay(1945, 4, 30) == (1, 'Monday') print weekDay(1900, 1, 1) == (1, 'Monday') print weekDay(1789, 7, 14) == (2, 'Tuesday')
如果date是date时间对象,这是一个解决scheme。
import datetime def dow(date): days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] dayNumber=date.weekday() print days[dayNumber]
date时间库有时会给strptime()提供错误,所以我切换到dateutil库。 下面是一个如何使用它的例子:
from dateutil import parser parser.parse('January 11, 2010').strftime("%a")
你从这里得到的输出是'Mon'
。 如果您希望输出为“星期一”,请使用以下内容:
parser.parse('January 11, 2010').strftime("%A")
这对我来说工作很快。 我在使用date时间库时遇到问题,因为我想存储星期几名称而不是星期几号码,而使用date时间库的格式导致问题。 如果你没有这个问题,太棒了! 如果你是,你可以无限期地去这个,因为它有一个简单的语法。 希望这可以帮助。
假设你有一天,一个月和一年,你可以这样做:
import datetime DayL = ['Mon','Tues','Wednes','Thurs','Fri','Satur','Sun'] date = DayL[datetime.date(year,month,day).weekday()] + 'day' #Set day, month, year to your value #Now, date is set as an actual day, not a number from 0 to 6. print(date)
如果你有理由避免使用date时间模块,那么这个函数将起作用。
注意:从Julian到Gregorian日历的变化假定是在1582年发生的。如果您的日历不是这样,那么在年份> 1582时相应地更改该行。
def dow(year,month,day): """ day of week, Sunday = 1, Saturday = 7 http://en.wikipedia.org/wiki/Zeller%27s_congruence """ m, q = month, day if m == 1: m = 13 year -= 1 elif m == 2: m = 14 year -= 1 K = year % 100 J = year // 100 f = (q + int(13*(m + 1)/5.0) + K + int(K/4.0)) fg = f + int(J/4.0) - 2 * J fj = f + 5 - J if year > 1582: h = fg % 7 else: h = fj % 7 if h == 0: h = 7 return h
为了让星期天为1到星期六为7,这是你的问题最简单的解决scheme:
datetime.date.today().toordinal()%7 + 1
他们全部:
import datetime today = datetime.date.today() sunday = today - datetime.timedelta(today.weekday()+1) for i in range(7): tmp_date = sunday + datetime.timedelta(i) print tmp_date.toordinal()%7 + 1, '==', tmp_date.strftime('%A')
输出:
1 == Sunday 2 == Monday 3 == Tuesday 4 == Wednesday 5 == Thursday 6 == Friday 7 == Saturday
这里是如何将date列表转换为date
import datetime,time ls={'1/1/2007','1/2/2017'} dt=datetime.datetime.strptime(ls[1], "%m/%d/%Y") print(dt) print(dt.month) print(dt.year)
import datetime a = datetime.datetime.today().weekday() if a == 0: print("It's Monday.") elif a == 1: print("It's Tuesday.") elif a == 2: print("It's Wednesday.") elif a == 3: print("It's Thursday.") elif a ==4: print("It's Friday.") elif a == 5: print("It's Saturday.") elif a == 6: print("It's Sunday.") # please like
🙂