Thread: Trying to find month/day from day of year

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

    Trying to find month/day from day of year

    So in one of our assignments we have to have someone input the day of the year (ex. 59) and have it output the month and day (ex. 2/28). All we have to work with is an array that declares how many days are in each month.

    I thought I had it beat with this code (for February):

    Code:
    else if((daysNmonth[1] + daysNmonth[2]) - dayOfYear >= 0)
    {
             dayOfMonth = daysNmonth[1] + daysNmonth[2] - dayOfYear;
             Month = 2;
             printf("Day %d of the year is %d/%d", dayOfYear, Month, dayOfMonth);
    }
    But then I realized it's counting backwards! For example, putting in 57 gives me 2/2. Is there an easier way to do this?

    Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So your daysInMonth variable needs to be how large, do you think?

    And what else do you think you need to do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Well my DaysNmonth is already defined, an array of 13 objects, because daysNmonth[0] is set to 0, and we ignore it. So daysNmonth[1] - daysNmonth[12] = the days of the month, such as 31, 28, etc..

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, and how are you going to know which month it is that you want? If you have 100 days into the year, how do you figure out that this means April?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Leap Year Problems
    By jc99 in forum C Programming
    Replies: 13
    Last Post: 06-21-2009, 04:02 AM
  2. Help with Easter Day Calculator
    By chrandbar in forum C Programming
    Replies: 10
    Last Post: 03-26-2009, 12:40 PM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  4. need current day, month, year
    By ihatejava in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2002, 11:41 AM
  5. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM