Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Two date function like
26/04/2016 - 31/08/1998= dd/mm/YYYY.
Please anyone program it.
I have tried several logics but didn't succeed.

What I have tried:

I have tried several logics but didn't succeed
Posted
Updated 6-Aug-17 23:43pm

At first you have to parse the input strings to get a numeric date value that can be subtracted. Which parsing function to use depends on the operating system and the optionally used framework.

With Linux you might use strptime(3) - Linux man page[^] and convert the resulting struct tm to time_t using mktime(3) - Linux man page[^]. Those time_t values can then be subtracted and converted back to other date types and finally formatted as string.

With Windows and MFC applications, you can use the COleDateTime Class[^] which provides the ParseDateTime function, the subtraction operator (there is also a version returning a COleDateTimeSpan Class[^] ), and format functions.

Note also that subtracting two dates results in a time span which is usually not formatted as "dd/mm/yy" but as number of days, months, or years (or a combination like "n years and m days").

So it makes no sense to provide code while you did not tell us about the used OS and framework, and how to display the result.
 
Share this answer
 
v2
Comments
Member 13349389 7-Aug-17 11:27am    
Windows 8 op,
Output should be in the format of dd/mm/yyyy
Jochen Arndt 7-Aug-17 12:42pm    
With MFC?
Than use the COleDateTime class as suggested, get the difference as COleDateTimeSpan and get the days by calling GetTotalDays(). From that you can calculate years, months, and remaining days.
 
Share this answer
 
You can't subtract two dates and get a date: it isn't logically possible.
When you subtract two dates, you get a time span - a number of years, months, or days between them.
That number of days may represent something - your age for example but it isn't of itself a date, and it can't be converted to a date without being referenced to another date.

Think about what you are trying to do, and decide exactly what you need - this is not going to work.
 
Share this answer
 
Comments
Nelek 7-Aug-17 6:15am    
But you can still put that time span in format dd/mm/yy ;)

I would reverse it to "yy, mm, dd" though
OriginalGriff 7-Aug-17 6:22am    
And even then, it's confusing!
I don't think he has thought about what he's trying to do at all...
Nelek 7-Aug-17 6:47am    
Actually... neither do I

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900