Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on library management software
so i need the difference of date between date on which book is issued and date on which book is returned so that i can calculate fine.
both date should be taken from system directly,not from user side.so i need a code.
Posted

Then you can use the GetSystemTimeAsFileTime[^] function.

FILETIME is a struct, but you can use an __int64 since the memory layout matches.

__int64 currentDateAndTime;
GetSystemTimeAsFileTime((LPFILETIME)&currentDateAndTime);

SYSTEMTIME systime;
FileTimeToSystemTime((LPFILETIME)&currentDateAndTime,&systime);


Doing the calculations on an __int64 is pretty easy ...

Convert to SYSTEMTIME[^] when you need to access the various elements of the date and time information.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sandeep Mewara 23-Jun-12 10:01am    
5! Good answer.
Espen Harlinn 23-Jun-12 10:03am    
Thank you, Sandeep :-D
Richard MacCutchan 23-Jun-12 10:39am    
Why not just use the simple functions - see my amended response.
Espen Harlinn 23-Jun-12 10:48am    
No reason at all, using time and difftime is a portable solution. :-)

For work, that is things I get paid for, I combine this solution, that is my, with your initial solution. Implementing classes similar to System.DateTime and System.TimeSpan is a fairly straight forward operation based on using an __int64.
You did not specify whether it is for MFC C++ or something else, so here's the MFC version of it...

1. Get date/time when the book was issued:
C++
CTime bookIssued = CTime::GetCurrentTime();


2. Get date/time when the book was returned:
C++
CTime bookReturned = CTime::GetCurrentTime();


3. Calculate the difference in days:
C++
CTimeSpan diff = bookReturned - bookIssued;
__int64 days = diff.GetDays();


Your variable days now has the number of days that you need.
 
Share this answer
 
v4
Read up on System.DateTime[^] and System.TimeSpan[^].

[edit]
As Espen & Vitaly pointed out this is a C++ question so my answer is not relevant, sorry. However using the time()[^] and difftime()[^] functions of the CRT library you can achieve the same objectives.
[/edit]
 
Share this answer
 
v2
Comments
Espen Harlinn 23-Jun-12 10:03am    
5'ed! that's how to do it if he is using C++/CLI :-D
Vitaly Tomilov 23-Jun-12 10:09am    
Irrelevant to C++.
Richard MacCutchan 23-Jun-12 10:34am    
Oops!
The boost date/time library Boost date/time[^] works really well and it's portable. It's pretty extensive and fearsome but the Date and Date Duration sections are all you need.
 
Share this answer
 

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