PDA

View Full Version : Get the date from day of year and year details



gfernandes
16th May 2014, 14:56
Hi,

This might be easy, but I couldn't find a solution.

If I have the day of year example 365 and the year 2013. Is it possible to convert it to date format 31.12.2013 in Qt? I didn't find anything for conversion from day of year. I tried using

QDate date_test = QDate::fromString("2014365","yyyyddd"); But it is invalid as the documents say ddd format means abbreviated localized day name (e.g. 'Mon' to 'Sun'). Is there any other way I can go about it in Qt?

Thank you in advance.

Radek
16th May 2014, 15:09
Use Julian period. First construct a QDate for 01.01.year, then convert to Julian date, then add DayInTheYear - 1, then convert back to "normal" date.

gfernandes
16th May 2014, 15:12
Hi,

Oh, I had done the same. Was looking for qt functions rather than this work around. Since we can get day of year from qdate I assumed there maybe a way to do the opposite. Thanks anyway :)

Just adding the work around I had done earlier to convert day of year, year to qdate
day_of_year = 365;
year = 2013;
QString test_date = QString::number(year);
test_date.append("0101");
QDate date = QDate::fromString(test_date,"yyyyMMdd");
date = date.addDays(day_of_year-1);

anda_skoa
16th May 2014, 15:33
QDateTime::addDays()?

Cheers,
_

ChrisW67
17th May 2014, 00:41
Specifically,


QDate date = QDate(year, 1, 1).addDays(dayOfYear - 1);