Weeks between two dates limit
-
Hi,
Do you mean the editor should not allow that or the user should get a message ?
-
Hi,
I have an application that when the user enters a date by entering the week of the year and the year, it cannot be older than N weeks (N should be 52 weeks or higher).
Cannot find a way to go using QDate.
Any idea?
Thanks,
@ollarch said in Weeks between two dates limit:
user enters a date by entering the week of the year and the year
What is the date of a week? First day of that ISO week/year number combo? Last day?
Are you defining your week number differently to ISO?(N should be 52 weeks or higher).
If the intent is that the entered date is not more than a year old also consider that some years have 53 ISO weeks.
-
@ollarch said in Weeks between two dates limit:
user enters a date by entering the week of the year and the year
What is the date of a week? First day of that ISO week/year number combo? Last day?
Are you defining your week number differently to ISO?(N should be 52 weeks or higher).
If the intent is that the entered date is not more than a year old also consider that some years have 53 ISO weeks.
Hi,
I have solved it using this approach:
QDate qCurrentDate = QDate::currentDate(); QDate qOldDate = qCurrentDate.addDays(-7 * m_qMaxWeeks);
Then, iterating on the qOldDate adding 7 days on each iteration you can get the weekNumber and the year. Here you can check if the input weekNumber and Year is on a valid range.
Thanks,
-
Hi,
I have solved it using this approach:
QDate qCurrentDate = QDate::currentDate(); QDate qOldDate = qCurrentDate.addDays(-7 * m_qMaxWeeks);
Then, iterating on the qOldDate adding 7 days on each iteration you can get the weekNumber and the year. Here you can check if the input weekNumber and Year is on a valid range.
Thanks,
@ollarch said in Weeks between two dates limit:
iterating on the qOldDate adding 7 days on each iteration
I'm not quite sure what you mean here. But "iterating" up to 52/53 times to discover something about a "week number" sounds very inefficient when presumably it could be calculated with some kind of subtraction and a division by 7. Or maybe using int QDate::weekNumber() or perhaps qint64 QDate::daysTo(). Having said that, you won't notice 50 iterations if you are just validating an interactive user input so if you are happy with this or can't be bothered to improve that is OK :)
-
Hi,
Maybe it can be improved thinking a little bit more.
Think on that the user enters the week number and the year, not really a date with a day, month and year. So, the week number and year cannot be older than N weeks from current date.
Using qint64 QDate::daysTo() maybe can help.
Thanks,