Welcome, guest | Sign In | My Account | Store | Cart

This function compares the day of the week of today to the day of the week passed.

Python, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/env python

def test_for_day(target_day):
    """ 
    Accepts a weekday and tests if today is that weekday.
    """
    import time
    # Get the date object of today's date:
    todays_date = time.localtime().tm_wday
    # Form a dictionary of the days of the week, starting on Monday
    # since this is the time module's assumption:
    date_dict = dict(enumerate('Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split()))
    # Find the weekday of today's date and compare to target:
    if date_dict[todays_date] == target_day:
        print "Today is the target (%s)." % target_day
    else:
        print "Today is %s, not %s." % (date_dict[todays_date], target_day)

I used this to check whether or not a script should perform a certain task to only be performed on a given weekday, regardless of what date it was.

Created by Samuel Huckins on Sat, 17 Jan 2009 (MIT)
Python recipes (4591)
Samuel Huckins's recipes (1)

Required Modules

Other Information and Tasks