Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. find diffrence between current time and birthday as a number of days.

find diffrence between current time and birthday as a number of days.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialhelp
20 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    quartaela wrote:

    but the problem is i read the birthday string

    As I said before you will need to convert the string to its constituent numbers using strtoi() (sorry not strtod()) and then use those numbers to populate a tm structure from which you can get the time_t value via mktime(). The documentation for all these functions is available on MSDN starting here[^]. This may all sound long and involved but I'm afraid that is the reality of programming.

    The best things in life are not things.

    Q Offline
    Q Offline
    quartaela
    wrote on last edited by
    #11

    well i almost near the solution. i use atoi() (i can't use strtoi() cause i guess compiler or O/S doesn't compatible with this function). so this the example code

    int main(void) {

    char c\[11\] = "2000/10/15";
    
           printf("the number is %d", atoi(c));
    return 0;
    

    }

    and the output is 2000. the problem is how can i take other numbers "10" and "15". i guess when i take this numbers i can able to solve the problem.

    L D 2 Replies Last reply
    0
    • Q quartaela

      well i almost near the solution. i use atoi() (i can't use strtoi() cause i guess compiler or O/S doesn't compatible with this function). so this the example code

      int main(void) {

      char c\[11\] = "2000/10/15";
      
             printf("the number is %d", atoi(c));
      return 0;
      

      }

      and the output is 2000. the problem is how can i take other numbers "10" and "15". i guess when i take this numbers i can able to solve the problem.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #12

      You need to split the string into its constituent parts using strtok(). I would suggest you spend some more time learning some of the more common library functions before going further with your project. Go back to the link[^] I gave you and read through all the alphabetic list so you know (more or less) what functions are available, then you know where to look when you have specific requirements.

      The best things in life are not things.

      Q 1 Reply Last reply
      0
      • L Lost User

        You need to split the string into its constituent parts using strtok(). I would suggest you spend some more time learning some of the more common library functions before going further with your project. Go back to the link[^] I gave you and read through all the alphabetic list so you know (more or less) what functions are available, then you know where to look when you have specific requirements.

        The best things in life are not things.

        Q Offline
        Q Offline
        quartaela
        wrote on last edited by
        #13

        well finally i managed it :). thanks for your help mates :).

        1 Reply Last reply
        0
        • Q quartaela

          well i almost near the solution. i use atoi() (i can't use strtoi() cause i guess compiler or O/S doesn't compatible with this function). so this the example code

          int main(void) {

          char c\[11\] = "2000/10/15";
          
                 printf("the number is %d", atoi(c));
          return 0;
          

          }

          and the output is 2000. the problem is how can i take other numbers "10" and "15". i guess when i take this numbers i can able to solve the problem.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #14

          What about:

          int y, m, d;
          sscanf(c, "%d/%d/%d", &y, &m, &d);

          It's not the best solution, but it does give you something to explore.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

          1 Reply Last reply
          0
          • L Luc Pattyn

            Date/times held in strings are no good for doing calculations, they are only good at interfacing to humans. Use the time_t type, it basically is the number of seconds since 01-JAN-1970. Once you have two time_t values, you can simply subtract them and compare to some constant, such as 7*24*60*60. [ADDED] POSIX has a nice strptime() function to turn a datetime string into a number; Windows itself hasn't (MFC has, boost has); so your best option may well be: - use sscanf to turn the string into struct tm; here[^] is an example that should work, provided the month is by number, not by name. - use mktime to turn that into a time_t - then either use difftime or simply subtract. [/ADDED] :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            modified on Tuesday, May 10, 2011 6:27 PM

            R Offline
            R Offline
            Ram Shelke
            wrote on last edited by
            #15

            MFC have CTimeSpan Class, add your date and time int one object and bithday time in other CTimeSpan objBithday(dd,mm,yy,hr,mm,ss); CTimeSpan objToday(dd,mm,yy,hr,mm,ss); CTimeSpan TimeDIff = objToday - objBithday; Try This............ : laugh:

            L L Q 3 Replies Last reply
            0
            • R Ram Shelke

              MFC have CTimeSpan Class, add your date and time int one object and bithday time in other CTimeSpan objBithday(dd,mm,yy,hr,mm,ss); CTimeSpan objToday(dd,mm,yy,hr,mm,ss); CTimeSpan TimeDIff = objToday - objBithday; Try This............ : laugh:

              L Offline
              L Offline
              Le rner
              wrote on last edited by
              #16

              chk this;

              CString today\_date\_str,birth\_date\_str;
              
              GetDlgItemText(IDC\_DATETIMEPICKER1, today\_date\_str);
              GetDlgItemText(IDC\_DATETIMEPICKER2, birth\_date\_str);
              
              m\_today\_dt\_ole.ParseDateTime(today\_date\_str,0,LANG\_USER\_DEFAULT);
              m\_birth\_dt\_ole.ParseDateTime(birth\_date\_str,0,LANG\_USER\_DEFAULT);
              
              CString chk\_dt1=m\_today\_dt\_ole.Format(\_T("%d/%m/%Y"));
              CString chk\_dt2=m\_birth\_dt\_ole.Format(\_T("%d/%m/%Y"));
              
              m\_today\_dt\_ole.ParseDateTime(chk\_dt1,0,LANG\_USER\_DEFAULT);
              m\_birth\_dt\_ole.ParseDateTime(chk\_dt2,0,LANG\_USER\_DEFAULT);
              
              COleDateTimeSpan daydiff=(m\_today\_dt\_ole-m\_birth\_dt\_ole);
              
              int noofday=0;
              
              noofday=(int)daydiff.GetTotalDays();
              if(noofday<=0)
              {
              	noofday=-(noofday);
              }
              noofday++;
              CString Value ;
              Value.Format(\_T("%d"),noofday);
              
              Q 1 Reply Last reply
              0
              • R Ram Shelke

                MFC have CTimeSpan Class, add your date and time int one object and bithday time in other CTimeSpan objBithday(dd,mm,yy,hr,mm,ss); CTimeSpan objToday(dd,mm,yy,hr,mm,ss); CTimeSpan TimeDIff = objToday - objBithday; Try This............ : laugh:

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #17

                I'm not the one having a problem with datetimes, I merely try to help out the OP. And I don't think he is using or willing to use MFC. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                1 Reply Last reply
                0
                • L Le rner

                  chk this;

                  CString today\_date\_str,birth\_date\_str;
                  
                  GetDlgItemText(IDC\_DATETIMEPICKER1, today\_date\_str);
                  GetDlgItemText(IDC\_DATETIMEPICKER2, birth\_date\_str);
                  
                  m\_today\_dt\_ole.ParseDateTime(today\_date\_str,0,LANG\_USER\_DEFAULT);
                  m\_birth\_dt\_ole.ParseDateTime(birth\_date\_str,0,LANG\_USER\_DEFAULT);
                  
                  CString chk\_dt1=m\_today\_dt\_ole.Format(\_T("%d/%m/%Y"));
                  CString chk\_dt2=m\_birth\_dt\_ole.Format(\_T("%d/%m/%Y"));
                  
                  m\_today\_dt\_ole.ParseDateTime(chk\_dt1,0,LANG\_USER\_DEFAULT);
                  m\_birth\_dt\_ole.ParseDateTime(chk\_dt2,0,LANG\_USER\_DEFAULT);
                  
                  COleDateTimeSpan daydiff=(m\_today\_dt\_ole-m\_birth\_dt\_ole);
                  
                  int noofday=0;
                  
                  noofday=(int)daydiff.GetTotalDays();
                  if(noofday<=0)
                  {
                  	noofday=-(noofday);
                  }
                  noofday++;
                  CString Value ;
                  Value.Format(\_T("%d"),noofday);
                  
                  Q Offline
                  Q Offline
                  quartaela
                  wrote on last edited by
                  #18

                  well mate i guess this is C++ or C#_? cause i can't understand the codes. i am working with C :). but thanks for your help

                  1 Reply Last reply
                  0
                  • R Ram Shelke

                    MFC have CTimeSpan Class, add your date and time int one object and bithday time in other CTimeSpan objBithday(dd,mm,yy,hr,mm,ss); CTimeSpan objToday(dd,mm,yy,hr,mm,ss); CTimeSpan TimeDIff = objToday - objBithday; Try This............ : laugh:

                    Q Offline
                    Q Offline
                    quartaela
                    wrote on last edited by
                    #19

                    and also i guess this is for C#_?. :).

                    1 Reply Last reply
                    0
                    • Q quartaela

                      hi friends i am trying to find the contacts who have a birthday in the next 7 days in my list. i managed get the current time as a string but i am stucked here. i know i must compare only number of the month and day. so use subtraction and if the result is <=7 i will show the contact. but i don't know how to do this. i appreciated if you can help me. and here is the my example code.

                      #include <stdio.h>
                      #include <time.h>
                      #include <string.h>

                      int main( )
                      {
                      char buff[11];
                      char birth[11]="2011-05-15";

                      int res;
                      
                      time\_t now = time(NULL);
                      strftime(buff, 20, "%Y-%m-%d\\n", localtime(&now));
                      
                               
                               res = difftime(mktime(birth), now);
                      	  
                      	  printf("%d", res);
                      	  
                      	 return 0;
                      

                      }

                      T Offline
                      T Offline
                      turboscrew
                      wrote on last edited by
                      #20

                      If you want to be exact, you need them all: days, months and years. The uears because of the leap day in February. The year is a leap year if the year is divisible by four, but not by 100, except if it's divisible by 400. Otherwise you might want to calculate days from the start of the year (takes care of different length months). Just have a table of days in each month and remember to add a day in february if it's a leap year.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups