简体   繁体   中英

How to obtain first Friday from a given QDate?

Consider a Qdate from

QDate Mydate = ui->dateEdit->date();

For example, suppose we choose 2018/07/14 (today).

How to obtain the day of the first Friday (in this case, 6) on the chosen month (in this case, July)?

I suspect we have to use Mydate.dayOfWeek() computations.

There is probably a neater solution, but:

  1. Subtract dayOfWeek for current date/day from dayOfMonth.
  2. Add 5 (for Friday).
  3. If -ve add 7 or if +ve answer is modulus 7.

Code:

dayOfWeekToday = MyDate.dayOfWeek()
firstFriday = MyDate.day() - dayOfWeekToday + 5
firstFriday = (firstFriday <= 0) ? firstFriday + 7 : firstFriday % 7

添加到尼克的答案,需要有一个特殊情况,其中MyDate.day() - dayOfWeekToday + 5的结果可以被 7 整除。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM