M
Matt
Given a date, how to find the beginning date and ending date of that week
please advise!
please advise!
Matt said:Given a date, how to find the beginning date and ending date of that week
please advise!
Richard Heathfield said:Initialise a struct tm like this:
struct tm tmt = {0};
Populate the fields you know. You will need, at a minimum, the day,
month, and year. Months are 0-11, and you will need to subtract 1900
from the year.
Yes.
Pass to mktime(). That will give you a time_t.
Pass its address to localtime(), giving you a pointer to a struct tm.
Read that struct tm's tm_wday field, where 0 means Sunday and 6 means
Saturday. If it's, say, 3, you know it's Wednesday. Decide what you
mean by "beginning of week". If you think Sunday is the first day of
the week, for example, then you know you're three away from the
beginning of the week. So subtract three from the monthday in the
struct tm, and pass it back to mktime() to normalise it.
Then you can pass it to localtime() - again! - to get a more useful
form. Use a similar technique for the end of the week.
Perhaps I'm making this more complicated than it need be, but I don't
see a way to make it shorter.
Simon said:But you don't need the time_t. Throw it away.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.