Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I convert char string to date?

Status
Not open for further replies.

Plato2

Programmer
Dec 6, 2002
192
US
E.g.
I ahve the following:
char time[20];
time="12/06/02";
How can I convert to date this string?
Thanks in advance!
 
hi, if with "date" you mean time_t C standard type to manage
date/time, you can use the mktime function.

char in_date[20]="12/06/02";

struct tm TM ;
int M,D,Y;
char buf[3];

memset( &TM, 0, sizeof(TM) );
memset( buf, 0, 3 );

memcpy( buf, in_date+0, 2 ); M = atoi( buf ) ;
memcpy( buf, in_date+3, 2 ); D = atoi( buf ) ;
memcpy( buf, in_date+6, 2 ); Y = atoi( buf ) ;

TM.tm_mday = D ; // Day of the month - [1,31]
TM.tm_mon = M-1 ; // Months since January - [0,11]
TM.tm_year = D+100 ; // Years since 1900

return mktime( &TM ) ;

BYE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top