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

Recommended Answers

All 11 Replies

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

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????

Try time function to get the current time.

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;
}

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 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.

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.

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..

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!

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();
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.