1,105,371 Community Members

Date function in C

Member Avatar
lonely_girl
Light Poster
29 posts since Nov 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi again
can u tell me how can i call the date function in "c"?

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

There's no "date" function in C. Be more specific or don't be surprised when nobody answers your question.

Member Avatar
lonely_girl
Light Poster
29 posts since Nov 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

well what i meant was that if i wanna use the date in my c so how can i do that? or let me explain wat m workin on..
m making a program that gives the age in the form of "years-months-days" and as input first it will need the current date and then the date of birth so for that i need some function that gives the current date or not????

Member Avatar
mad93
Newbie Poster
4 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Try time function to get the current time.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

http://www.cplusplus.com/reference/clibrary/ctime/

Here's an unrelated example to get you started:

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

const char *suffix ( int thing )
{
  switch ( thing ) {
    case 1: return "st";
    case 2: return "nd";
    case 3: return "rd";
    default: return "th";
  }
}

int main ( void )
{
  time_t now = time ( NULL );
  struct tm *date = localtime ( &now );

  printf ( "Today is the %d%s day of the %d%s month in the year %d\n",
    date->tm_mday, suffix ( date->tm_mday ),
    date->tm_mon + 1, suffix ( date->tm_mon + 1 ),
    date->tm_year + 1900 );

  return 0;
}
Member Avatar
lonely_girl
Light Poster
29 posts since Nov 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok i need a little guide about my program.. How i take my start?
taking the current year,current month,current date,birth year, birth month, birth date input and giving the age in the form of "years-months-days" suppose "18 years-2months-21days"...

Member Avatar
death_oclock
Posting Whiz
392 posts since Apr 2006
Reputation Points: 103 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

You can use a tm structure for the date of birth, convert it with mktime(), and subtract that from time(). Then convert your answer back into a tm struct.

Member Avatar
csurfer
Posting Pro
564 posts since Jan 2009
Reputation Points: 422 [?]
Q&As Helped to Solve: 89 [?]
Skill Endorsements: 2 [?]
 
0
 

ok i need a little guide about my program.. How i take my start?
taking the current year,current month,current date,birth year, birth month, birth date input and giving the age in the form of "years-months-days" suppose "18 years-2months-21days"...

You can take "18-2-21" as a string input into string str and then use the
strtok(<str address>,"-") function to tokenise this and get it converted into integer by atoi() function to extract the year month and date values into integer variables.

Then just by using struct tm with clock() and using inbuilt int variables as tm_year,tm_mon,tm_mday you can continue.

Member Avatar
lonely_girl
Light Poster
29 posts since Nov 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You can take "18-2-21" as a string input into string str and then use the
strtok(<str address>,"-") function to tokenise this and get it converted into integer by atoi() function to extract the year month and date values into integer variables.

Then just by using struct tm with clock() and using inbuilt int variables as tm_year,tm_mon,tm_mday you can continue.

18-2-21
was just an example the user can give any input..

Member Avatar
death_oclock
Posting Whiz
392 posts since Apr 2006
Reputation Points: 103 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
0
 

Then instead of setting the input variable to "18-2-21" (obviously not what csurfer intended), use fgets to get a line of user input. Also you could use sscanf() as long as you make sure to check its return value!

Member Avatar
lonely_girl
Light Poster
29 posts since Nov 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hey honestly saying i didnt get ur points as m very beginner so it takes time for me to understand. I succeeded in what i wanted to make and now m sharing it with u plz post ur comments and also if there r any errors.

#include<stdio.h>
#include<conio.h>
void main ()
{
int cy,cm,cd,by,bm,bd,age,month,days;
clrscr();
printf("Enter current year:");
scanf("%d",&cy);
printf("\nEnter current month:");
scanf("%d",&cm);
printf("\nEnter current date:");
scanf("%d",&cd);
printf("\nEnter birth year:");
scanf("%d",&by);
printf("\nEnter birth month:");
scanf("%d",&bm);
printf("\nEnter birth date:");
scanf("%d",&bd);
if (cm<bm && cd<bd)
{
days=30+cd-bd;
month=cm-1+12-bm;
age=cy-1-by;
printf("\n%d-%d-%d",age,month,days);
}
if ((cm>bm)&&(cd<bd))
{
days=30+cd-bd;
month=cm-1-bm;
age=cy-by;
printf("\n%d-%d-%d",age,month,days);
}
if ((cm<bm)&&(cd>bd))
{
days=cd-bd;
month=12+cm-bm;
age=cy-1-by;
printf("\n%d-%d-%d",age,month,days);
}
if((cm>=bm)&&(cd>=bd))
{
days=cd-bd;
month=cm-bm;
age=cy-by;
printf("\n%d-%d-%d",age,month,days);
}
getch();
}
Member Avatar
csurfer
Posting Pro
564 posts since Jan 2009
Reputation Points: 422 [?]
Q&As Helped to Solve: 89 [?]
Skill Endorsements: 2 [?]
 
0
 

18-2-21
was just an example the user can give any input..

"18-2-21" represents the format of input you need to take.You can generalize it as "<years>-<moths>-<days>" separated by just by "-" so that you can use "-" as the delimiter in the strtok( ,<delimiter>) function for your purpose. Other information are already in the earlier post.

Question Answered as of 5 Years Ago by Narue, death_oclock, csurfer and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article