Compare Date and show the difference
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Compare Date and show the difference

  1. #1
    Join Date
    Dec 2010
    Posts
    11

    Compare Date and show the difference

    I have a file, 'date-list.txt' which has date in it. For example , 20 May 2012 .

    Now I want to compare this date with current system date and show the difference in days .

    How can i compare dates and shows difference in days in C++.?

    Im really a c++ beginner . So , if possible , please show and guide me with codes . Thanks in advanced .

  2. #2
    Join Date
    Apr 2008
    Posts
    12

    Re: Compare Date and show the difference

    When I approach a situation like this, I start thinking through a lot of the details before trying to get the code written. (Sorry there's no code.)

    There are many things to consider.

    We need to know a bit more about how 'date-list.txt' will be formatted.
    1) Is it only going to have one date, or many dates? If many, will they only be listed one per line?
    2) Is it only going to use that format, or could your program be given a different formatted date such as "05/20/2012"?
    3) May is a tough example, because months are often abbreviated as three characters, but May starts out as only three characters. If your program is to be given September, will it be given "Sep" or "September"?

    Do we need our program to be flexible, or rigid?
    1) If 'date-list.txt' will have many dates, one listed per line, do we want to be prepared to see a blank line with no date on it?
    2) Do we want to be prepared to see unexpected date formats as mentioned above, or just give an error? Even if 'date-list.txt' should only be using months as three character abbreviations or their full name, do we want to be prepared to take either?

    Are we to assume the date in 'date-list.txt' is in the same time zone as the current system's time? (Pretty much have to, just pointing this out.)

  3. #3
    Join Date
    Apr 2008
    Posts
    12

    Re: Compare Date and show the difference

    Furthermore, and this could be taking the scenario a bit too far especially if it's for a programming class, but does the current time come into play? We should consider that, since we don't have a time in 'date-list.txt'.

    If we are calculating the difference in days from "5/15/2012" to "5/20/2012 00:00:00", most people would consider that a 5 day difference.

    But, what if we are calculating the difference in days from "5/15/2012" to "5/20/2012 23:59:59"? People could have differences in opinion on whether that is a 5 day or 6 day difference, depending on how the calculated amount is going to be used. You could say technically it's a 5 day difference. But, are we OK with skipping that 6th day due to a second on the current time clock? Maybe we are, and maybe we aren't. Maybe we need to have 'date-list.txt' include a time (if we control creating it), or maybe we need to calculate fractions of a day rather than whole days.

    What I'm trying to point out is that in these two scenarios, is effectively how we want to round this off.

    If it's for a programming class, we're certainly going to ignore the time aspect all together, if the wording of the assignment never mentions time.

    However, if it's for a real world situation, we need to consider what context the result we calculate is going to be used in.
    Last edited by darlingm; May 20th, 2012 at 10:17 PM.

  4. #4
    Join Date
    Dec 2010
    Posts
    11

    Re: Compare Date and show the difference

    Hi ,

    Thanks for a fast feedback . Actually , this is for my project . So , I still dont have any data files created . I just assumed the date formats . From your experience and knowledge , how I can implement these easily .

    My project details :-

    Consumer Good Expiry

    *User can key-in goods data with its expiry date . For example , Egg x20 20/03/2012 - Item name Qty Expiry Date .
    *After user key-in data for the first time , the data file will created(fstream) . Then , update the data file if user key-in for second time and onwards .
    *Then , user able to compare the expiry dates in data file with current date . The compare result should show the difference of days between data date and current date .

    So , which format of date that you suggest and how to implement the code to work the way I want . Thanks .

  5. #5
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Compare Date and show the difference

    Quote Originally Posted by mrexp21 View Post
    Hi ,

    Thanks for a fast feedback . Actually , this is for my project . So , I still dont have any data files created . I just assumed the date formats .
    The most important part is the date format. It's not up to us to tell you the best format, you have to decide what is the best format for your purposes.

    Second, how were you going to get the difference between two dates? The classical way to do this is to convert a "normal-looking" date into a Julian date, and then subtract the two Julian dates giving the number of days. Please google "Julian date".

    So given all of this, you have many options -- save dates as Julian dates in the file, or save the "normal-looking" date in the file and convert to Julian to do the calculations, etc. etc.
    So , which format of date that you suggest and how to implement the code to work the way I want . Thanks .
    Again, no one is going to implement any code for you or tell you the best way (if you're assignment is for you to determine how you're going to save the dates to a file). You must decide what the date format is and then go from there, or you have to clearly state what the format will be in the file.

    All we can do is give you ideas -- that's it. The only thing that I suggest is that computing the difference between dates is usually done using Julian date calculations.

    Regards,

    Paul McKenzie

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,204

    Re: Compare Date and show the difference

    Quote Originally Posted by darlingm View Post
    When I approach a situation like this, I start thinking through a lot of the details before trying to get the code written. (Sorry there's no code.)

    There are many things to consider.

    We need to know a bit more about how 'date-list.txt' will be formatted.
    1) Is it only going to have one date, or many dates? If many, will they only be listed one per line?
    2) Is it only going to use that format, or could your program be given a different formatted date such as "05/20/2012"?
    3) May is a tough example, because months are often abbreviated as three characters, but May starts out as only three characters. If your program is to be given September, will it be given "Sep" or "September"?

    Do we need our program to be flexible, or rigid?
    1) If 'date-list.txt' will have many dates, one listed per line, do we want to be prepared to see a blank line with no date on it?
    2) Do we want to be prepared to see unexpected date formats as mentioned above, or just give an error? Even if 'date-list.txt' should only be using months as three character abbreviations or their full name, do we want to be prepared to take either?

    Are we to assume the date in 'date-list.txt' is in the same time zone as the current system's time? (Pretty much have to, just pointing this out.)
    I'm going to respectfully disagree and say I don't think any of that is really important. The crux of the problem is converting the dates into a format that allows you to calculate the difference. While you need to know the text file format, the OP already gave the format he expects and as a simple exercise, it should be assumed that the file format will be consistent. With that assumption, parsing it would be trivial.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center