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

current date - 3 days ago

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
Hi,

I need to find the date 3 days ago from the current date (current date - 3). Could anyone tell me how I can acheive this. Any help would be greatly appreciated.
 
If you are writing the code in VC++ using MFC then you can use the functions like CTIME,CSPAN which gives the current and even you can get date three days
Hope this may help you.
 
I am writing the code in C, Could you tell me how I can write this in C.
 
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
char tmpbuf[128], ampm[] = &quot;AM&quot;;
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();

/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( &quot;OS time:\t\t\t\t%s\n&quot;, tmpbuf );
_strdate( tmpbuf );
printf( &quot;OS date:\t\t\t\t%s\n&quot;, tmpbuf );

/* Get UNIX-style time and display as number and string. */
time( &ltime );
printf( &quot;Time in seconds since UTC 1/1/70:\t%ld\n&quot;, ltime );
printf( &quot;UNIX time and date:\t\t\t%s&quot;, ctime( &ltime ) );

/* Display UTC. */
gmt = gmtime( &ltime );
printf( &quot;Coordinated universal time:\t\t%s&quot;, asctime( gmt ) );

/* Convert to time structure and adjust for PM if necessary. */
today = localtime( &ltime );
if( today->tm_hour > 12 )
{
strcpy( ampm, &quot;PM&quot; );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;

/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( &quot;12-hour time:\t\t\t\t%.8s %s\n&quot;,
asctime( today ) + 11, ampm );

/* Print additional time information. */
_ftime( &tstruct );
printf( &quot;Plus milliseconds:\t\t\t%u\n&quot;, tstruct.millitm );
printf( &quot;Zone difference in seconds from UTC:\t%u\n&quot;,
tstruct.timezone );
printf( &quot;Time zone name:\t\t\t\t%s\n&quot;, _tzname[0] );
printf( &quot;Daylight savings:\t\t\t%s\n&quot;,
tstruct.dstflag ? &quot;YES&quot; : &quot;NO&quot; );

/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( &quot;Christmas\t\t\t\t%s\n&quot;, asctime( &xmas ) );

/* Use time structure to build a customized time string. */
today = localtime( &ltime );

/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
&quot;Today is %A, day %d of %B in the year %Y.\n&quot;, today );
printf( tmpbuf );
 
Check with Time.c header file you will find a suitable function for your problem
 
Try this (on Solaris):

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

main()
{
time_t ts;
struct tm *gmt;

/*Time Now*/
time(&ts);
gmt = gmtime(&ts);

/*Loose 3 days*/
gmt->tm_mday -= 3;

/*Make new time*/
ts = mktime(gmt);
gmt = gmtime(&ts);

printf(&quot;%s&quot;, asctime(gmt));

return 0;
}

Hope this gives you something to think about. ;-)
 
Huskers:

The date question in &quot;C&quot; was addressed in another thread in this forum:

thread205-246034

Regards,

Ed
 
Get the current number of seconds since the epoch using time(, subtract 3 x 42 x 60 x 60 from that (3 days in seconds) then convert the result to a time structure using localtime().

Cheers - Gavin
 
Sorry - last post suffered from typos (new keyboard!) the number of seconds to subtract should obviously be 3 x 24 x 60 x 60 !
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top