Returns the previous month of an informed date in Qt

Asked

Viewed 114 times

3

How do I return the previous month of the date reported in Qt, has some function in the QDate what does it do? I just found the one that adds addMonths. Example: month 04 reported returns month 03.

1 answer

4


Yes, the method month() do this.

QDate data(2015, 4, 6);
int mes = data.month();

According to the comment below what is desired is to subtract months, so use own addMonths(), just use a negative value.

QDate data(2015, 4, 6);
int mesAnterior = data.addMonths(-1);

I put in the Github for future reference.

  • bigown is now. How do I return the previous month(which is already past)? Example date.Month() returns month 04, now I would like it to return 03.

  • It was not clear in the question that it was the previous month that you wished.

  • Thanks bigown. It worked now. data.addMonths(-1).toString("dd/MM/yyyy");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.