please, explain me, how do this thing: I have a week number (52, for example) and year (2012). So, how I can get the days number (monday - 24, tuesday - 25, etc). Yes, I read this, but I cant understand, how to do it.
Thanks.
I would do it like this:
from datetime import date, timedelta
def get_weekdays(year, week):
january_first = date(year, 1, 1)
monday_date = january_first + timedelta(days=week * 7 - january_first.weekday())
# monday, tuesday, .. sunday
return [(monday_date + timedelta(days=d)).day for d in range(7)]
(my weeks start at monday)
Related
I'm trying to take a date, for example Aug 22, 2017 02:00 PM EDT
and get the month, day, year from it.
month = re.findall(r'', date)[0]
day = re.findall(r'', date)[0]
year = re.findall(r'', date)[0]
I've started with something like this:
(.*)(?<=[a-zA-Z]{3}\s)
for the month. Is there a better way to do this?
You need to first convert to datetime and then extract the needed values like this (reusing the example):
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
print(datetime_object.year, datetime_object.month, datetime_object.day)
From what I can see you probably won't need to specify the format but pass the string directly to the datetime.strptime function.
I have a massive spreadsheet in which all dates are written this way:
2 days ago
9 days ago
34 days ago
54 days ago
etc.
Is there a clever Python way to convert these data to the actual dates, if I tell Python what date '1 day ago' is?
Use timedelta.
Extract the value from that string in your spreadsheet and then use
d = date.today() - timedelta(days_to_subtract)
If the input date format may slightly vary (human input) then you could use parsedatetime module to parse human-readable date/time text into datetime objects:
#!/usr/bin/env python
import sys
from datetime import datetime
import parsedatetime # $ pip install parsedatetime
now = datetime(2015, 3, 8) # the reference date
cal = parsedatetime.Calendar()
for line in sys.stdin: # at most one date per line
dt, type = cal.parseDT(line, now)
if type > 0:
print(dt)
Output
2015-03-06 00:00:00
2015-02-27 00:00:00
2015-02-02 00:00:00
2015-01-13 00:00:00
I have a model as follows:
class WorkTime(models.Model):
person = models.ForeignKey(Person)
entry_date = models.DateField(default=datetime.datetime.now(), verbose_name='date')
start_time = models.TimeField(verbose_name='start')
end_time = models.TimeField(verbose_name='end')
With data as follows:
person, date, start, end
1 01/01/2014 08:00 12:00
1 01/01/2014 13:00 18:00
1 02/01/2014 08:00 12:00
1 02/01/2014 13:00 18:00
1 03/01/2014 08:00 16:00
1 01/02/2014 08:30 12:00
1 01/02/2014 13:00 18:00
2 01/01/2014 09:00 13:00
2 01/01/2014 14:00 18:00
How would one sum up the time delta (i.e. end_time - start_time) and GROUP BY person to show the hours worked by person, as below?
person, hours
1 34:30
2 08:00
I don't know a good way to do this in the ORM. It's pretty straight forward to build a dictionary by iterating through a queryset.
from collections import defaultdict
from datetime import datetime
totals = defaultdict()
work_times = WorkTime.objects.
for work_time in work_times:
totals[work_time.person] += datetime.combine(work_time.entry_date, work_time.end_time) - datetime.combine(work_time.entry_date, work_time.start_time)
# print results
for person, total_time in totals.items():
# total_time will be a timedelta, you can do some more work to return hours and minutes
print person, total_time
Depending on your usage, the performance of this might be good enough.
This has been a while, but maybe someone will stumble upon this.
You can do it this way:
from django.db.models import F, Sum
queryset = queryset.values('person').annotate(sum_delta=Sum(F('end_time')-F('start_time')).order_by('person__id')
This will sum up all timedeltas for each person and you dont have to store extra stuff in the db.
This is my first question on Stack.
I am working on a booking site that relies heavily on searching and finding full weeks of accommodation. Most user searches will be on weeknumber of the year, eg. week 27 for the first week of july.
It is important that the user does not need to fill in year when searching for accommodation, and so the only thing we will get from the user is the weeknumber.
How can I get the year from the week given by the user considering that it always has to be the next upcoming occurrence of that week number?
(There is a gotcha in this. I could get the upcoming week 27 by doing something like this:
def week
week = 27
Date.commercial(Date.current.year + 1, week, 1) # gives the first day of the week
end
But that would only be right until the 1 of January, after that it would be looking for week 27 of 2015.)
You could compare the current calendar week with Date.current.cweek (Reference) with your number.
require 'active_support/core_ext' # Already included in Rails
def calendar_week(week)
now = Date.current
year = now.cweek < week ? now.year : now.year + 1
Date.commercial(year, week, 1)
end
p calendar_week(49)
# => Mon, 02 Dec 2013
p calendar_week(1)
# => Mon, 30 Dec 2013 # don't know if that's the way calendar weeks are counted
p calendar_week(27)
# => Mon, 30 Jun 2014
I see Google's example code listing dates for January, but the chart is displaying dates for February!
On my test machine, it is doing the same thing. I've told it to display dates for September, but it is displaying dates for October instead!
Can anyone else confirm this as happening?
http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html
The months in the javascript date are 0 based not 1 based. So 0 is Jan, 1 is Feb, etc.
See
http://www.w3schools.com/js/js_obj_date.asp
and you might want to check out
http://en.wikipedia.org/wiki/Off-by-one_error
ECMA-262 5ed, pp.165:
15.9.1.4 Month Number
Months are identified by an integer in the range 0 to 11, inclusive.
No, you're not losing your mind. The month in the Javascript Date object is zero-indexed. That means:
0 = January
1 = February
2 = March
3 = April
4 = May
5 = June
6 = July
7 = August
8 = September
9 = October
10 = November
11 = December