如何转换特定日期的工作日?

时间:2015-12-03 21:11:03

标签: python html calendar weekday

我被困了好几个小时;有人可以帮我解决这个问题吗?

创建一个迷你网络应用程序,告诉用户特定日期的工作日。

要确定星期几,您可以使用日历模块中的功能。可以像下面这样调用工作日函数:

工作日= calendar.weekday(y,m,d) ...假设您有变量y,m和d中的年,月和日。这将在变量工作日中输入0-6的整数,其中0表示星期一,1表示星期二,依此类推。

使用一系列if语句在生成的页面上生成这样的输出(当然有适当的工作日 - 如果工作日为0,这就是你产生的):

这是我的第一篇文章,它不会让我上传我的代码,因为这里的格式不同但我有第一部分(import.cgi,导入日历,和 form = cgi.FieldStorage()part)然后是内容类型和doctype,然后是CGI脚本

然后是int代码

然后是工作日部分的if语句

import.cgi
import calendar
form = cgi.FieldStorage()

#CGI Script 
print 
year = int(form.getvalue("year")) 
month = int(form.getvalue("month")) 
day = int(form.getvalue("day")) 
weekday = calendar.weekday(y, m, d) 
if weekday == 0: 
    print ("<p> That's monday </p>") 
if weekday == 1: 
    print ("<p> That's tuesday </p>") 
if weekday == 2: 
    print ("<p> That's wednesday </p>") 
if weekday == 3: 
    print ("<p> That's thursday </p>") 
if weekday == 4: 
    print ("<p> That's friday </p>") 
if weekday == 5: 
    print ("<p> That's saturday </p>") 
if weekday == 6: 
    print ("<p> That's sunday </p>") 

2 个答案:

答案 0 :(得分:0)

从你的代码开始,我不得不将y,m,d替换为year,month,day作为calendar.weekday的参数以使其工作。这是你被困的地方吗?

import calendar
year = 2015 
month = 12 
day = 3 
weekday = calendar.weekday(year, month, day) 
if weekday == 0: 
   print ("<p> That's monday </p>") 
if weekday == 1: 
   print ("<p> That's tuesday </p>") 
if weekday == 2: 
   print ("<p> That's wednesday </p>") 
if weekday == 3: 
   print ("<p> That's thursday </p>") 
if weekday == 4: 
   print ("<p> That's friday </p>") 
if weekday == 5: 
   print ("<p> That's saturday </p>") 
if weekday == 6: 
   print ("<p> That's sunday </p>") 

答案 1 :(得分:0)

Python喜欢提供您可能需要的东西......所以我们可以使用他们在库中拥有的东西。试试这个脚本:

import time
t = (2015, 12, 3, 0, 0, 0, 0, 0, 0) # t is Dec 3 2015 -- replace with whatever you like
q = time.mktime (t)                 #convert to floating point rep, which is needed by...
r = time.gmtime (q)                 #convert to struct time, which is needed by...
print (time.strftime ("%A", r))     #formats time as a string.  %A means "weekday"

这将于周四打印,因为2015-12-3是星期四。

根据需要调整自己的脚本。

有关time.strftime的更多信息,请参阅http://www.tutorialspoint.com/python/time_strftime.htm