Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1047
  • Last Modified:

DateAdd

is there a function that add part of date specific value, like DateAdd of Basic ?
0
BlueAndWhite
Asked:
BlueAndWhite
  • 2
1 Solution
 
jkrCommented:
No, but you can use 'mktime()', e.g.


/* MKTIME.C: The example takes a number of days
 * as input and returns the time, the current
 * date, and the specified number of days.
 */

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

void main( void )
{
   struct tm when;
   time_t now, result;
   int    days;

   time( &now );
   when = *localtime( &now );
   printf( "Current time is %s\n", asctime( &when ) );
   printf( "How many days to look ahead: " );
   scanf( "%d", &days );

   when.tm_mday = when.tm_mday + days;
   if( (result = mktime( &when )) != (time_t)-1 )
      printf( "In %d days the time will be %s\n",
              days, asctime( &when ) );
   else
      perror( "mktime failed" );
}


Output

Current time is Tue May 03 12:45:47 1994

How many days to look ahead: 29
In 29 days the time will be Wed Jun 01 12:45:47 1994


0
 
BlueAndWhiteAuthor Commented:
in VC
0
 
BlueAndWhiteAuthor Commented:
Thanks
0
 
mnashadkaCommented:
If you're using MFC and CTime or COleDateTime, you can add or subtract based on the CTimeSpan or COleDateTimeSpan classes:

CTime now = CTime::GetCurrentTime();
now += CTimeSpan(0, 1, 0, 0); // Add 0 days, 1 hour, 0 minutes, and 0 seconds

If you're using the basic time_t, you can always add the number of seconds:
time_t now = time(NULL);
now += 2 * 24 * 60 * 60; // Add 2 days in seconds

Good luck.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now