How can I find out all the dates of a given day in this month?

I need to write a program in C, which, given the name of the day, returns all the dates in this month in which the day will be included.

For example, if the entry is Sunday, the exit should be: 5,12,19,26 (which are Sunday days this month.)

Does anyone have any ideas how to do this? I've tried a lot.

+4
source share
3 answers

You can use the time () function to get the current time.

Then use the localtime () function to get a structure (struct tm) with information (year, month, day, ...) of the current time.

From "struct tm" get tm_mday and tm_wday. Use these fields to determine the next or previous Sunday. for example, if tm_mday is 12 and tm_wday is 3 (Wednesday), then we are now on the 9th of this month - Sunday (12-3 = 9). From this number, simply add or subtract 7 to get all the rest of the Sundays.

+2
source

Do you need to know this for a certain year? Or is it just this year? If you need to know this for any year, you can list "days per month", having one for leap years and one for off-peak years.

You just need to know what day of the week the year began (i.e., Monday, Tuesday, etc.)

You will have at least 5 dates for any month, so you can have an array with a fixed int length.

You know that the gregorian calendar is repeated every 400 years, and that if year X started on day β€œY”, then year X + 1 will start with day (β€œY” + 1)% 7, if x is not a leap year, if it is a leap year, it will start on the day ("Y" + 2). which can give you the first date of any year, and knowing how many days you have all the months for any year, you can easily get the date on which this month begins ("Monday", etc.).

Then all you have to do is something like

 int offset = 0; int i; while (myDate + offset != monthStartingDate) { offset++; } i = offset + monthStartingDate; 

(myDate is the day of the week, and monthStartingDate is the day of the week on the first day of this month)

when you exit this cycle, you will have the first occurrence, then you simply add 7 until I go beyond the month.

you can add each i to the array.

 int res[5] = {0,0,0,0,0} for ( ; i < daysOfMonth(month, year); i += 7) { int res[i / 7] = i; } 

then you just return res.

Oh, I don’t know that you could use date functions: P I think the idea of ​​the exercise was fulfilling C: P

+1
source

1) Take the string (name of the day of the week) from the input (use scanf or gets )

2) Convert it to a number (find it in the table on weekdays using the loop and strcmp ), assign 0 on Sunday, 1 for Monday ...

3) Get the current time using the time function and convert it to tm struct using the localtime function

4) From tm struct calculates the first day in the current month of the given working day.

 first_mday_of_given_wday = (tm_struct.tm_mday + given_wday - tm_struct.tm_wday + 6) % 7 + 1 

5) Find out how many days in the current month. For this:

  • put 1 in tm_mday and 0 in tm_isdst your tm struct
  • duplicate structure
  • increase by 1 tm_mon in the duplicate (see the last month! in this case, increase tm_year by 1 and set tm_mon to 1)
  • convert strothcs booth to time_t using the mktime function and calculate the difference (just subtract these time_t values), convert the result from seconds to several days (divide by 60 * 60 * 24)

6) Run the loop, although the calculated range is:

 for (i = first_mday_of_given_wday; i <= num_days_in_month; i += 7) printf("%d, ", i) 

The fifth step can be omitted in certain situations. We know that a month can have from 28 to 31 days. If any of the hypothetical days 29,30,31 of the current month cannot be this working day, we can assume that the current month has 28 days.
So simple: suppose we have 28 days in the current month if first_mday_of_given_wday more than 3, otherwise calculate the number as shown in step 5.

+1
source

All Articles