I need to write a code to be able to interpret the input date to calendar system.
First input is the date and second input is the first day of the year. I dont know how to start. I hope someone could help me.

Thanks

12/10/2011
S
Output:
October, 2011
Su Mo Tu We Th Fr Sa
__________________1
2__3___4__5__6__7__8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Dani AI

Generated

A compact, practical plan that fills the gap between the original request and the replies from , and .

Start by making the inputs unambiguous: parse the date into day/month/year (detect DD/MM vs MM/DD by checking if the first field > 12) and accept the "first day of the year" as either a full weekday name or an explicit index 0..6 (0=Sunday..6=Saturday). Note: Jan 1, 2011 was a Saturday, which matches the calendar shown in the original post when October 1, 2011 falls on a Saturday.

Algorithm (stepwise)

  • Validate the date and year, apply the Gregorian leap-year rule (divisible by 4, except centuries not divisible by 400).
  • Compute daysBeforeMonth = sum of days in months 1..(month-1) for that year.
  • Convert provided weekday-of-Jan-1 to index jan1 (0..6).
  • firstWeekdayOfMonth = (jan1 + daysBeforeMonth) % 7.
  • Print header (month name, year), weekday labels, then pad the first week with spaces for firstWeekdayOfMonth and print day numbers with fixed width; break the line after Saturday.

Minimal C++ sketch (core helpers and printing idea):

bool isLeap(int y){ return (y%4==0 && (y%100!=0 || y%400==0)); }
int daysInMonth(int m,int y){ int md[]={31,28,31,30,31,30,31,31,30,31,30,31}; int d=md[m-1]; if(m==2 && isLeap(y)) d=29; return d; }

int firstWeekdayOfMonth(int jan1, int month, int year){
    int sum=0;
    for(int m=1;m<month;++m) sum += daysInMonth(m,year);
    return (jan1 + sum) % 7; // 0=Sun ... 6=Sat
}

/* printing: print header, "Su Mo Tu We Th Fr Sa", then print '   ' firstWeekday times, then for d=1..dim:
   printf("%2d", d); if((first + d) % 7 == 0) printf("\n"); else printf(" ");
*/

Troubleshooting and notes

  • If the provided "first day" is ambiguous ('S'), accept 'Su'/'Sat' or prefer numeric input.
  • For verification, compare the computed weekday with platform APIs (std::tm + mktime or Zeller's congruence) when input reliability is a concern.
  • Test several known dates (e.g., Jan 1, 2011 = Saturday; Oct 1, 2011 = Saturday) and edge cases around leap years and month boundaries.

Recommended Answers

All 2 Replies

Post your code. Maybe we can help. This might .

commented: Saw no reason for someone to down-vote your post. +0

this is more than what you need

go through the code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.