Results 1 to 3 of 3

Thread: how to convert days to months

  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Qt products
    Platforms
    Windows

    Question how to convert days to months

    i have a date of joining of an employee and i want to know till current date how many MONTHS he/she has worked in the company..

    if i take months ~= 30 days the answer i get is not accurate..

    Can someone plz guide me how can i convert days to MONTHS for above requirement..

    Thanx
    ad3d

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to convert days to months

    Unless you know the date of hire or the date of the query, you cannot get an exact answer.

    If you know one of those parameters, you simply have to loop over a table of month lengths and add up the results, plus whatever slop remains in the first or final partial month.

    This assumes that the period covered is entirely within one year. If not, or if the period spans several years, you will have to perform corrections for leap years as well.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to convert days to months

    You could use a better approximation of the number of days in a month. The Gregorian calendar averages to 365.2425 days per year, so an average month is 30.4369 days. For many purposes this is close enough.

    If it is for payroll purposes you are better going the long way with something like:
    Qt Code:
    1. int wholeMonths(const QDate &from, const QDate &to)
    2. {
    3. if (from.isValid() && to.isValid() && to > from) {
    4. int months = qMax(1, (to.year() - from.year() - 1) * 12);
    5. while (from.addMonths(months) <= to)
    6. ++months;
    7. return months - 1;
    8. }
    9. else
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 14th February 2011 at 22:32.

Similar Threads

  1. Replies: 1
    Last Post: 4th February 2011, 14:47
  2. Replies: 0
    Last Post: 11th August 2010, 18:32
  3. General qt days..
    By Y-Less in forum Qt Programming
    Replies: 8
    Last Post: 10th April 2008, 21:41
  4. who can help me? It cost me many days.
    By diaryon0 in forum General Programming
    Replies: 1
    Last Post: 23rd April 2006, 09:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.