Solved

DateAdd

Posted on 2003-10-23
4
1,031 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
[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
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

733 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