日期时间功能已停止工作

时间:2017-09-06 14:23:09

标签: python datetime

我从我的Python脚本中收到错误 - 不确定是否因为传递给它的日期有问题,或者因为我在导入的datetime模块中使用了错误的方法。

代码

import webapp2
import json
import urllib
import urllib2
from datetime import datetime, timedelta

from google.appengine.ext import ndb
from google.appengine.api import users

import db
import shift_calendar
import templates
import authenticate


def is_BST(input_date):
    if input_date.month in range(4,9):
        return True
    if input_date.month in [11,12,1,2]:
        return False
    # Find start and end dates for current year
    current_year = input_date.year

    for day in range(25,32):
        if datetime.datetime(current_year,3,day).weekday()==6:
            BST_start = datetime.datetime(current_year,3,day,1)
        if datetime.datetime(current_year,10,day).weekday()==6:
            BST_end = datetime.datetime(current_year,10,day,1)

    if (input_date > BST_start) and (input_date < BST_end):
        return True

    return False

错误

  if datetime.datetime(current_year,3,day).weekday()==6:
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

附录

将datetime.now()传递给上述脚本的函数是:

if is_BST(datetime.now()) == True:
    displaytime = (datetime.now() + timedelta(hours=1)).strftime('%H:%M:%S')
    today = (datetime.now() + timedelta(hours=1)).strftime('%Y-%m-%d')
    starthour = (datetime.now() + timedelta(hours=1)).strftime('%H')
    timenow = (datetime.now() + timedelta(hours=1)).strftime('%H')
    endhour = (datetime.now() + timedelta(hours=2)).strftime('%H')
else:
    displaytime = datetime.now().strftime('%H:%M:%S')
    starthour = datetime.now().strftime('%H')
    today = datetime.now().strftime('%Y-%m-%d')
    timenow = datetime.now().strftime('%H')
    endhour = (datetime.now() + timedelta(hours=1)).strftime('%H')

1 个答案:

答案 0 :(得分:2)

您已从datetime导入datetime,因此无需在代码中调用datetime.datetime

将导入语句更改为import datetime或将代码更改为datetime(...)

顺便说一句,您可能还希望使用arrow代替datetime

for day in range(25, 32):
    if datetime(current_year, 3, day).weekday() == 6:
        BST_start = datetime(current_year, 3, day, 1)
    if datetime(current_year, 10, day).weekday() == 6:
        BST_end = datetime(current_year, 10, day, 1)