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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

to get date and time.... 1

Status
Not open for further replies.

leela74

Programmer
May 11, 2001
26
0
0
IN
Hi,

With the following program I amgetting date and time in a buffer.

#include <time.h>
#include <stdio.h>
void main(void)
{
char datebuf[9];
char timebuf[9];

_strdate(datebuf);
_strtime(timebuf);
printf(&quot;Date: %s Time: %s\n&quot;,datebuf,timebuf);
}

The date and time are in this format mm/dd/yy & HH:MM:SS

But I am to format the above date and time and I want it to be mmdd and HHMM

so the datebuf = mmdd
timebuf = hhmm

how do i do this in C.

Thanks in advance

Sridhar
 
For the date use strrchr() to find the last / in the string datebuf and replace it with a NULL or 0
For the time use strrchr() to find the last : in the string timebuf and replace it with a NULL or 0
Then do your
printf(&quot;Date: %s Time: %s\n&quot;,datebuf,timebuf);



Kim_Christensen@telus.net
 
Hi,

I came to know with strrchr() function at what place the / character is... but I couldn't able to replace it with NULL

How do I do this....

Thanks in advance

Sridhar
 
Hi! Sridhar.

Do a simpler thing. replace datebuf[2] with datebuf[3], then datebuf[3] with datebuf[4]and place '\0' in datebuf[4].
Do the same with timebuf. U have ddmm and hhmm in datebuf and timebuf now.

Regards,
SwapSawe.
 
Hi, SwapSawe

Its a simple thing but I tried and still trying to replace the remaining part with NULL but not succeeded.... Still unable to replace with NULL

What could be wrong...

Sridhar
 
for(i=2;i<5;i++)
{
if(i==5)
{
datebuf='\0';
timebuf='\0';
break;
}
datebuf=datebuf[i+1];
timebuf=timebuf[i+1];
}
datebuf[0]=d
datebuf[1]=d
datebuf[2]=/
datebuf[3]=m
datebuf[4]=m
datebuf[5]=/
datebuf[6]=y
datebuf[7]=y
datebuf[8]='\0' slash zero.
Hope now its done.
s-)
SwapSawe.

 
There's no need to use non-standard functions for this and you can save yourself the trouble of modifying a resultant string by using localtime() and sprintf():

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

int main(void)
{
char dt[5];
char tm[5];
struct tm *t_ptr;
time_t tmm=time(NULL);

t_ptr=localtime(&tmm);

sprintf(dt,&quot;%02d%02d&quot;,t_ptr->tm_mon+1,t_ptr->tm_mday);
sprintf(tm,&quot;%02d%02d&quot;,t_ptr->tm_hour,t_ptr->tm_min);

printf(&quot;date: %s; time: %s\n&quot;,dt,tm);
return 0;
}

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top