Date counter in Python 3 - python

I want to make a date counter, but I don't want to use datetime. I have made a list of the days of the month:
monthDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
This way, if a month number is input, then I can search through the list for the same number and find the corresponding amount of days in that month e.g. monthDays[7] will find me 31.
The input of my date is of the form: dd/mm/yyyy, so I just use date[0:2] to extract the date ... and so on. First I check if the input date is in the future - fine.
Then I say, if the month is equal to today's month and the year is equal to today's year, then the difference is just the difference of the two day values.
I'm seriously a little bit stuck from here...
I've got, if today's month is less than or equal to the input month AND today's year is equal to the input year, then the elapsed days is the sum of:
the days left in the current month; and
the days between today's month and the input month
taken away from:
the difference of the two day values.
Can anybody help from here onwards? I want to be able to calculate the days elapsed between today and ANY date in the future.

If the date is after the current year, then you'll need to calculate the number of days left in this year, then, the number of days into the new year of the new date and any whole years inbetween.
Don't forget, monthDays[2] can be 28 or 29, depending on the year. Leap years are identified by years where:
The year is evenly divisible by 4;
If the year can be evenly divided by 100, it is NOT a leap year, unless;
The year is also evenly divisible by 400. Then it is a leap year.
(Source: http://www.timeanddate.com/date/leapyear.html)

Related

Creating a day of year column overriding the leap day in a leap year

I have a large database of climate variables - daily values of temp, humidity etc. I have a timestamp column %Y%m%d. I have removed leap days, as I need uniform 365 days for each of my years. I want to add a new column called 'day_of_year' with 1 to 365 for each year for as many years as I have in my database. How can I accomplish this in python, any pointers, please?
If I use the day of year function from pandas, I get 59 for feb 28 and get 61 for Mar 1. Is there a way to override the leap year, as I have dropped the leap day and get 60 for Mar 1?
Use pandas' day of year function, but instead of giving it the real timestamp, e.g. "2022-11-27", give it "2021-" + timestamp[-5:]. This will give you the altered number as if the timestamp was not a leap year.

How to create a dictionary where the keys are all the week numbers within a year and values as lists of the dates of each weekday within a week?

I am creating a calendar for a schedule.
How would I go about creating a dictionary containing the week number's of a year as key's and the value's would be a list of all the dates of days for that given week.
I want to create a loop to achieve the following.
1 Get each week of the year.
2 Get week days of each week and convert them to their dates and put
them into a list like such
weekdays ['20/05/2019', '21/05/2019', '22/05/2019', '23/05/2019',
'24/05/2019', '25/05/2019', '26/05/2019']
3 Create new key and value for the weeks dictionary using the weeknumber as key and the weekdays as the value.
weeks[weeknum] = weekdays
The contents of the weeks dictionary would look like this but with all 52 weeks.
weeks {22: weekdays, 23: weekdays... etc}
I can get a weeknumber and a weekday of a given date using isocalander but this wouldn't be enough to achieve what I want to do.
What is the best way to approach this?
I'd iterate over the days in a year and then add them to a list for each week number. If you use defaultdict, you can do that without having to initialize the dict for the week numbers:
import datetime
from collections import defaultdict
date = datetime.date(2019, 1, 1)
enddate = datetime.date(2019, 12, 31)
days_in_each_week = defaultdict(list)
while date < enddate:
days_in_each_week[date.isocalendar()[1]].append(date.strftime("%d/%m/%Y"))
date += datetime.timedelta(days=1)
print(days_in_each_week)

Calculating week number of year (dealing with first week of year)

I am trying to convert the date into week number of year.
In my case, I cannot include the day of one year into another year.
The isocalendar() works fine to find week number, however, it assumes some rules: if the first week in January has less than 4 days, it is counted as the last week of the year before.
So, this function returns:
date(2016, 1, 1).isocalendar()[1]
53
Is there some way, using this function, to change this, to return week 0 instead of week 53 (from previous year) ?
how about this?
import datetime
datetime.date(2016, 1, 1).strftime("%U")

Python - How to get the date after entering a number between 1-365

How would you write a Python program that prompts a user for a year, a day of the year (an integer between 1 and either 365 or 366 to account for leap year), and gives the month, day and year?
You can use datetime.timedelta to add a number of days to a starting date. Use datetime.datetime to create the starting date with January 1st of the year.

Algorithm to get the current week number (not ISO)

I'm looking for an easy way to get the current week number of the year in Python. I'm well aware of the datetime.datetime.isocalendar() function in the standard library, but this function stipulates that week 1 is the first Monday of the new year. My dilemma is that I'm using Sunday as a starting point for each week, and if Sunday is for example December 27 and January 1st appears at some point during that week, I need to represent that week as week 1 (and year 2015).
I thought of doing something like (pseudocode):
if (Jan 1) - (current_sunday) < 7 days:
week_num = 1
And then storing that week number somewhere to iterate over next week. However, I feel that this is a very hackish method and would prefer something cleaner.
Generally to get the current week number (starts from Sunday):
>>> import datetime
>>> import calendar
>>> today = datetime.date.today())
>>> (int(today.strftime('%U')) + (datetime.date(today.year+1, 1, 1).weekday() != calendar.SUNDAY)) % 53
12
From the documentation of strftime('%U'):
"Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0."
Hence the modified code for your specific requirements. There isn't really a non-hacky way to do what you want.

Categories

Resources