Solved

DateAdd

Posted on 2003-10-23
4
1,022 Views
Last Modified: 2007-12-19
is there a function that add part of date specific value, like DateAdd of Basic ?
0
Comment
Question by:BlueAndWhite
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9608482
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
 

Author Comment

by:BlueAndWhite
ID: 9608485
in VC
0
 

Author Comment

by:BlueAndWhite
ID: 9608495
Thanks
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9610121
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now