current week / weeks in year - best practice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aljosa Mohorovic

    current week / weeks in year - best practice

    i use this to find out current week and total number of weeks for
    current year:
    now = datetime.now()
    weeks_in_year = int(date(now.ye ar, 12, 31).strftime("% W"))
    current_week = int(date(now.ye ar, now.month, now.day).strfti me("%W"))

    is this the best way or is there a better way?

    Aljosa Mohorovic
  • Diez B. Roggisch

    #2
    Re: current week / weeks in year - best practice

    Aljosa Mohorovic wrote:
    i use this to find out current week and total number of weeks for
    current year:
    now = datetime.now()
    weeks_in_year = int(date(now.ye ar, 12, 31).strftime("% W"))
    current_week = int(date(now.ye ar, now.month, now.day).strfti me("%W"))
    >
    is this the best way or is there a better way?
    Instead of datetime.now() use date.today(), which removes a lot of
    boilerplate.

    int(date.today( ).strftime("%W" ))

    Apart from that, I think it's the way to go.

    Diez

    Comment

    • Aljosa Mohorovic

      #3
      Re: current week / weeks in year - best practice

      On Jul 31, 3:58 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
      Instead of datetime.now() use date.today(), which removes a lot of
      boilerplate.
      >
      int(date.today( ).strftime("%W" ))
      >
      Apart from that, I think it's the way to go.
      what if i know current context week = 20 (example), what would be the
      best way to get datetime objects for first and last day of current
      context week?
      by "current context week" i don't mean current week for current year
      but current week when program is iterating all weeks in year.

      Aljosa

      Comment

      • Aljosa Mohorovic

        #4
        Re: current week / weeks in year - best practice

        On Jul 31, 5:42 pm, Aljosa Mohorovic <aljosa.mohoro. ..@gmail.com>
        wrote:
        what if i know current context week = 20 (example), what would be the
        best way to get datetime objects for first and last day of current
        context week?
        by "current context week" i don't mean current week for current year
        but current week when program is iterating all weeks in year.
        if w = current context week and now is current datetime object this
        is how i calculate days:
        first_day = datetime.strpti me("%s %s" % (now.year, str((w-1)*7)), "%Y
        %j")
        last_day = first_day + timedelta(days= 6)

        any comments on this?

        Aljosa Mohorovic

        Comment

        • Tim Roberts

          #5
          Re: current week / weeks in year - best practice

          Aljosa Mohorovic <aljosa.mohorov ic@gmail.comwro te:
          >
          >what if i know current context week = 20 (example), what would be the
          >best way to get datetime objects for first and last day of current
          >context week?
          >by "current context week" i don't mean current week for current year
          >but current week when program is iterating all weeks in year.
          One of the problems is that "current week of the year" is not a
          well-defined term. Some companies define the first week as the week that
          contains January 1. Some companies define the first week as the first full
          week, so that a partial Jan 1 week is actually part of the previous year.
          And you get the whole "does the week start on Sunday or Monday" debate as
          well.

          You need to make sure that the datetime functions match your business
          rules.
          --
          Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          • egbert

            #6
            Re: current week / weeks in year - best practice

            On Sat, Aug 02, 2008 at 07:46:49PM -0700, Dennis Lee Bieber wrote:
            <heh>
            >
            What is the meaning of <heh?
            e.
            --
            Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
            =============== =============== =============== =============== ============

            Comment

            Working...