Trying to make a countdown, where you input the date:
from datetime import datetime
year = int(input('Enter a year: '))
month = int(input('Enter a month: '))
day = int(input('Enter a day: '))
date = datetime.date(year, month, day)
countdown = date - datetime.now()
print(countdown)
The error is:
line 7, in <module>
date = datetime.date(year, month, day)
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object
Try this:
date = datetime(year, month, day).date()
countdown = date - datetime.now().date()
print(countdown)
That error is raised when an operation or function is applied to an object of inappropriate type.
A datetime object is a single object containing all the information from a date object and a time object.
A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions.
Try adding .date() to datetime object.
e.g.: datetime(year, month, day).date()
Related
I am making a simple program to calculate the difference between two dates: A specified date and the current date using datetime module.
def difference(current, until):
year, month, day = current.year, until.month, until.date
print("Year:", current.year, "Type:", type(current.year))
this_year = datetime.datetime(year, month, day)
return this_year - current
I can see that type(current.year) is an 'int'. However, when I try to make a new date, an error occurs. Output:
Year: 2023 Type: <class 'int'>
this_year = datetime.datetime(year, month, day)
TypeError: an integer is required (got type builtin_function_or_method)
tl;dr
Change
year, month, day = current.year, until.month, until.date
to
year, month, day = current.year, until.month, until.day
The current.year is definitely an integer. The issue is with your until.date variable that is getting assigned to day.
As mentioned in #chepner's comment: until.date is a bound method that returns a datetime.date object. Read more in the documentation about it here - https://docs.python.org/3/library/datetime.html#date-objects
Meanwhile, changing your until.date to until.day will fix your issue.
I have the following code
minDate = date(9999, 12, 31)
start = event.get('dtstart').dt
if isinstance(start, datetime.datetime):
newStart = start.date()
else:
newStart = start
if(newStart < minDate):
minDate = start
why am I getting this error as I have converted to date on both ends of the comparison
They are not the same type. From Docs:
class datetime.date
- An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: year, month, and day.
class datetime.datetime
- A combination of a date and a time. Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo
You will need to mitigate the comparison here:
if(newStart < minDate):
Where minDate is date type and newStart is dateTime
I have the date object which is as follows. Date: 2025-11-30. If I add 3 month to it using following code, it will raise an Exception as follows.
code:
def get_next_n_month_forward(date, n):
if date.month + n > 12:
next_month = date.replace(year=date.year + 1,
month=(date.month + n) % 12)
else:
next_month = date.replace(month=(date.month + n))
return next_month
exception:
ValueError: day is out of range for month
As far as I can understand from error it is because february does not have 30th day.
Question: How can I make it set to the last day of the month? !Note: In my case it would be 29th or 28th of February.
You can use timedelta object to increase and decrease dates
from datetime import timedelta
import datetime
d = datetime.datetime.today()
new_d = d + timedelta(days=1)
In order to set the date to the last day of month, you can set the date to the 1st of the next month and then decrease it by 1 day:
from datetime import timedelta
import datetime
day = datetime.datetime.today()
prev_month_last_day = day.replace(day=1) - timedelta(days=1)
So I'm trying to subtract one day from a users input of 2018-02-22 for example. Im a little bit stuck with line 5 - my friend who is is leaps and bounds ahead of me suggested I try this method for subtracting one day.
In my online lessons I haven't quite got to datetime yet so Im trying to ghetto it together by reading posts online but got a stuck with the error :
TypeError: descriptor 'date' of 'datetime.datetime' object needs an argument
from datetime import datetime, timedelta
date_entry = input('Enter a date in YYYY-MM-DD format')
year, month, day = map(int, date_entry.split('-'))
date1 = datetime.date()
new = date1.replace(day=date1.day-1, hour=1, minute=0, second=0, microsecond=0)
print (new)
So my aim is the get the out put 2018-02-21 if I put in 2018-02-22.
Any help would be amazing :)
First of all a timedeltais best used for the purpose of doing date arithmetcs. You import timedelta but don't use it.
day = timedelta(days=1)
newdate = input_datetime - day
Second problem is you're not initializing a date object properly. But in this case it would be better to use datetime as well as strptime to parse the datetime from the input string in a certain format.
date_entry = input('Enter a date in YYYY-MM-DD format')
input_date = datetime.strptime(date_entry, "%Y-%m-%d")
day = timedelta(days=1)
newdate = input_datetime - day
from datetime import datetime, timedelta
date_entry = input('Enter a date in YYYY-MM-DD format')
date1 = datetime.strptime(date_entry, '%Y-%m-%d')
print date1+timedelta(1)
Maybe you need something like this
from datetime import datetime, timedelta
date_entry = input('Enter a date in YYYY-MM-DD format ')
# convert date into datetime object
date1 = datetime.strptime(date_entry, "%Y-%m-%d")
new_date = date1 -timedelta(days=1) # subtract 1 day from date
# convert date into original string like format
new_date = datetime.strftime(new_date, "%Y-%m-%d")
print(new_date)
Output:
Enter a date in YYYY-MM-DD format 2017-01-01
'2016-12-31'
Hello sorry for the ambiguous title, here's what I want to do :
I have a string:
month = '1406'
that corresponds to the month of June, 2014.
How can I dynamically say that that string represents the month of June and I specifically want the last day of the month.
So I want to write it in the format: '%Y-%m-%d %H:%M:%S' and have:
'2014-06-30 00:00:00'
You get the last day of a given month with the calendar.monthrange() function. Turn your string into two integers (month, year), create a datetime object from the year, month and last day of the month:
from datetime import datetime
from calendar import monthrange
year, month = int(month[:2]), int(month[2:])
year += 2000 # assume this century
day = monthrange(year, month)[1]
dt = datetime(year, month, day) # time defaults to 00:00:00
Demo:
>>> from datetime import datetime
>>> from calendar import monthrange
>>> month = '1406'
>>> year, month = int(month[:2]), int(month[2:])
>>> year += 2000 # assume this century
>>> day = monthrange(year, month)[1]
>>> datetime(year, month, day)
datetime.datetime(2014, 6, 30, 0, 0)
>>> print datetime(year, month, day)
2014-06-30 00:00:00
The default string conversion for datetime objects fits your desired format already; you can make it explicit by calling str() on it or by using the datetime.datetime.isoformat() method, as you see fit.
This seems to do the trick:
import datetime
mon = '1406'
y, m = divmod(int(mon), 100)
dt = datetime.datetime(2000 + y + m // 12, m % 12 + 1, 1) - datetime.timedelta(days=1)
print dt