×

Discussion Board

Results 1 to 4 of 4

Thread: Day of Week?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Question Day of Week?

    Is there a way to get the day of the week from the time module? If not, does anyone have good code for getting it from the d//m/y or epoch seconds?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    18
    You can easily write the code yourself: pick
    a reference day (like Jan 01/05) where you know the day of the week and use the d/m/y data (or seconds) to compute the number N of days
    passed since reference day. Then compute
    N%7 (=remainder modulo 7). This gives you a number between 0 and 6 and hence the day of the week, where 0 is the day of the week of reference day. Until early 2008 there is no problem using d/m/y ;-)

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Thanks. Why didn't I think of that?

    K

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    From http://docs.python.org/lib/module-time.html
    The description for the time tuple is:

    0 tm_year (for example, 1993)
    1 tm_mon range [1,12]
    2 tm_mday range [1,31]
    3 tm_hour range [0,23]
    4 tm_min range [0,59]
    5 tm_sec range [0,61]; see (1) in strftime() description
    6 tm_wday range [0,6], Monday is 0
    7 tm_yday range [1,366]
    8 tm_isdst 0, 1 or -1; see below

    so you can grab tm_wday to get the weekday value.

    eg
    (yr, mo, da, h, m, s, wd, jd, ds) = \
    time.localtime(time.time())
    then use wd as index.

    even better, you don't have to get it right when you create the tuple
    eg:
    >>newTime = time.mktime((2005, 05, 29, 0, 0,0, 0, 0, -1))
    >>(yr, mo, da, h, m, s, wd, jd, ds) = \
    time.localtime(newTime)
    >>print wd
    6 (It's a sunday)
    >>print jd
    149

    Kate
    http://nokiapython.blogspot.com/
    Last edited by kateeveritt; 2005-05-30 at 00:29.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×