How Can I Select Date Based on Monday Date?

43 views
Skip to first unread message

Alexander Joseph

unread,
May 12, 2018, 3:00:29 PM5/12/18
to Django users
I'm building a timesheet app for employees and would like the employees to be able to select a day of the week date based on what the date was on Monday.

ie. the user inputs the Monday date of the timesheet, then for each row they need to fill out they can select the Day/Date for each day that week so that those hours they input get assigned to that specific day/date. The easiest way I can think of would be to make a list of choices in the Model such as ...

MONDAY = 'MO'
TUESDAY = 'TU'
WEDNESDAY = 'WE'
THURSDAY = 'TH'
FRIDAY = 'FR'
SATURDAY = 'SA'
SUNDAY = 'SU'

DAY_CHOICES = (
    (MONDAY, 'Monday'),
    (TUESDAY, 'Tuesday'),
    (WEDNESDAY, 'Wednesday'),
    (THURSDAY, 'Thursday'),
        
(FRIDAY, 'Friday'),
        
(SATURDAY, 'Saturday'),
       
(SUNDAY, 'Sunday'),
)
day = models.CharField(
    max_length=2,
    choices=DAY_CHOICES,
    default=MONDAY,
)

This would work except I also want to include the actual date of the day they select, based on the Monday date that they put in in another Model

Is there a library or package I can use to do this? 

Thanks!

Mike Dewhirst

unread,
May 13, 2018, 8:47:47 PM5/13/18
to Django users
Not sure. Have a look at baklabel, a utility I wrote many years ago and
while I use it every day I haven't looked at it in living memory. Source
is at https://pypi.org/project/baklabel/#files

It takes a date and returns a string being 'mon', tue' and so on except
'fri1', 'fri2' etc and it also cares about end-of-month and end-of-year.
The string is intended as a path fragment for labelling scripted backups.

The settings/options will allow a much more vanilla return string. You
could stop it counting fridays and omit end-of-month and end-of-year
detection.

The date arithmetic in there might be useful and extractable as separate
library.

>
> Thanks!
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6089eafa-7495-4ec4-a539-624cf95014b0%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/6089eafa-7495-4ec4-a539-624cf95014b0%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
May 13, 2018, 10:11:58 PM5/13/18
to django...@googlegroups.com
Thanks Mike, I will check that out!
I may have found another solution this afternoon too. Since the user puts in the week start date I think I might be able to use datedelta to the week start date so that Tuesday would be the week start date + deltadate (1), Wednesday would be the week start date + deltadate (2) and so on. Still need to check if this does what I'm thinking it will though. I'll check out baklabel too 


You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/cK-iY8YW6l8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Mike Dewhirst

unread,
May 13, 2018, 11:41:42 PM5/13/18
to django...@googlegroups.com
On 14/05/2018 12:11 PM, Alexander Joseph wrote:
> Thanks Mike, I will check that out!
> I may have found another solution this afternoon too. Since the user
> puts in the week start date I think I might be able to use datedelta
> to the week start date so that Tuesday would be the week start date +
> deltadate (1), Wednesday would be the week start date + deltadate (2)
> and so on. Still need to check if this does what I'm thinking it will
> though. I'll check out baklabel too

Now you mention it here is another utility I keep around ...

def when(days=0): """Function to return the current non-naive
datetime.""" dd = datetime.now() + timedelta(days=days) dday =
datetime.now(tz=pytz.utc) if days == 0: return dday ttime =
datetime.time(dday) tt = timezone.make_aware(datetime.combine(
datetime.fromordinal(dday.toordinal() + days), ttime), pytz.utc) return tt
> <mailto:django-users%2Bunsu...@googlegroups.com>
> > <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> > To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
> > <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com>>.
> > Visit this group at https://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/django-users/6089eafa-7495-4ec4-a539-624cf95014b0%40googlegroups.com
>
> >
> <https://groups.google.com/d/msgid/django-users/6089eafa-7495-4ec4-a539-624cf95014b0%40googlegroups.com?utm_medium=email&utm_source=footer>.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/cK-iY8YW6l8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email
> to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGJrYjYg%2BqT_Xsz%2Ba-QKCy-6Fat88N-fch-E_9F9oovp7yusXw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAGJrYjYg%2BqT_Xsz%2Ba-QKCy-6Fat88N-fch-E_9F9oovp7yusXw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Mike Dewhirst

unread,
May 13, 2018, 11:48:57 PM5/13/18
to django...@googlegroups.com
On 14/05/2018 12:11 PM, Alexander Joseph wrote:
> Thanks Mike, I will check that out!
> I may have found another solution this afternoon too. Since the user
> puts in the week start date I think I might be able to use datedelta
> to the week start date so that Tuesday would be the week start date +
> deltadate (1), Wednesday would be the week start date + deltadate (2)
> and so on. Still need to check if this does what I'm thinking it will
> though. I'll check out baklabel too

>> Now you mention it here is another utility I keep around ...
>>
>> def when(days=0): """Function to return the current non-naive datetime.""" dd = datetime.now() + timedelta(days=days) dday = datetime.now(tz=pytz.utc) if days == 0: return dday ttime = datetime.time(dday) tt = timezone.make_aware(datetime.combine( datetime.fromordinal(dday.toordinal() + days), ttime), pytz.utc) return tt
>>

What I really meant was ...

def when(days=0):
"""Function to return the current non-naive datetime."""
dd = datetime.now() + timedelta(days=days)
dday = datetime.now(tz=pytz.utc)
if days == 0:
return dday
ttime = datetime.time(dday)
tt = timezone.make_aware(
datetime.combine(
datetime.fromordinal(dday.toordinal() + days),
ttime
),
pytz.utc
)
return tt





>
>
> <mailto:django-users%2Bunsu...@googlegroups.com>
> > <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> > To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
> > <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com>>.
> > Visit this group at https://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/django-users/6089eafa-7495-4ec4-a539-624cf95014b0%40googlegroups.com
>
> >
> <https://groups.google.com/d/msgid/django-users/6089eafa-7495-4ec4-a539-624cf95014b0%40googlegroups.com?utm_medium=email&utm_source=footer>.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/cK-iY8YW6l8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email
> to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGJrYjYg%2BqT_Xsz%2Ba-QKCy-6Fat88N-fch-E_9F9oovp7yusXw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAGJrYjYg%2BqT_Xsz%2Ba-QKCy-6Fat88N-fch-E_9F9oovp7yusXw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Alexander Joseph

unread,
May 14, 2018, 1:34:38 PM5/14/18
to Django users
Ah yes, timedelta, thats what I meant instead of deltadate haha. Thanks again
Reply all
Reply to author
Forward
0 new messages