Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Posted on 2003-10-23
Medium Priority
1,043 Views
is there a function that add part of date specific value, like DateAdd of Basic ?
0
Question by:BlueAndWhite
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 86

Accepted Solution

jkr earned 500 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

ID: 9608485
in VC
0

Author Comment

ID: 9608495
Thanks
0

LVL 8

Expert Comment

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their waâ€¦
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.
###### Suggested Courses
Course of the Month10 days, 5 hours left to enroll