Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 71607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:54:52+00:00 2026-05-10T19:54:52+00:00

If I have a date, how do I calculate the week number for that

  • 0

If I have a date, how do I calculate the week number for that date within that year?

For example, in 2008, January 1st to January 6th are in week 1 and January 7th to the 13th are in week 2, so if my date was January 10th 2008, my week number would be 2.

An algorithm would be great to get me started and sample code would also help – I’m developing in C++ on Windows.

Related:

Getting week number off a date in MS SQL Server 2005?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:54:52+00:00Added an answer on May 10, 2026 at 7:54 pm

    Pseudocode:

    int julian = getDayOfYear(myDate)  // Jan 1 = 1, Jan 2 = 2, etc... int dow = getDayOfWeek(myDate)     // Sun = 0, Mon = 1, etc... int dowJan1 = getDayOfWeek('1/1/' + thisYear)   // find out first of year's day // int badWeekNum = (julian / 7) + 1  // Get our week# (wrong!  Don't use this) int weekNum = ((julian + 6) / 7)   // probably better.  CHECK THIS LINE. (See comments.) if (dow < dowJan1)                 // adjust for being after Saturday of week #1     ++weekNum; return (weekNum) 

    To clarify, this algorithm assumes you number your weeks like this:

    S  M  T  W  R  F  S             1  2  3    <-- week #1 4  5  6  7  8  9 10    <-- week #2 [etc.] 

    getDayOfWeek() and getDayOfYear() are standard date-object operations in most languages. If yours doesn’t have them, you can count-forward from some known date (Jan 1, 1970 is a common one), after looking up to see what day of the week it was.

    If you’re going to implement your own date counting routines, remember that years that are divisible by 100 are NOT leap years, unless they are also divisible by 400. So 1900 was not a leap year, but 2000 was. If you’re going to work far back in time, you have to mess with Gregorian vs Julian calendars, etc., see Wikipedia for loads of info on that.

    This link talks about date/time functions in Windows/C++ in greater detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this routine that calculates the seconds-to-date for a struct tm . On
I have a weird date rounding problem that hopefully someone can solve. My client
If I have records: Row Date, LocationID, Account 1 Jan 1, 2008 1 1000
Is it possible to create an sql statement that selects the week number (NOT
I have the date of birth of a person and want to calculate the
I have a date variable as 24-dec-08 . I want only the 08 component
I have a date and time column in my mysql table called start_date and
For PHP I have a date I want line wrapped. I have $date =
I'm a little blockheaded right now… I have a date string in european format
I am working on a project where the requirement is to have a date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.