find diffrence between current time and birthday as a number of days.
-
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 notstrtod()
) and then use those numbers to populate atm
structure from which you can get thetime_t
value viamktime()
. 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.
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.
-
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.
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.
-
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.
-
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.
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
-
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
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:
-
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:
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);
-
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:
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.
-
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);
-
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:
-
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;
}
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.