Date manipulation question

已答复 Date manipulation question

  • 2012年3月15日 16:08
     
     

    Using either cstring or string classes how do I manipulate a date?

    I would be getting the date in the format of 11/12/2009 with cin.getline

    and I want it to cout November 12, 2009.

    Any idea to get me started would be appreciated.


    • 已编辑 eddie174 2012年3月15日 16:15
    •  

全部回复

  • 2012年3月15日 16:17
     
     建议的答复
  • 2012年3月15日 16:54
     
     

    First thing why not simply use time_t or SYSTEMTIME etc to get the time .

     time_t rawtime;
     time ( &rawtime );

    or

     SYSTEMTIME st, lt;    
        GetSystemTime(&st);
        GetLocalTime(&lt);  

    and then calculate the difference between time and store it in string or do watever you want.

    Thanks


    Rupesh Shukla

  • 2012年3月15日 17:12
     
      包含代码

    Here's an example of a routine I wrote to parse a date format we use.  You'd have to adjust it, of course, to fit your date format.  I pass a CString into this routine.

    //
    // Converts an XML Timestamp to a CTime object
    // Timestamp format = CCYY-MM-DD HH:MM:SS
    //
    CTime FromTimeStamp(LPCTSTR cTimeStamp)
    {
    	CTime cTime = CTime::GetCurrentTime();
    	if(!(cTimeStamp == NULL || _tcslen(cTimeStamp) == 0)) {
    		int month, day, year, hours , minutes, seconds; 
    		_stscanf_s(cTimeStamp,_T("%d-%d-%d %d:%d:%d"),&year, &month, &day, &hours, &minutes, &seconds); 
    		if(year == 1969) { // Epoch for CTime is 12/31/1969, but this doesn't work on all implemntations
    				// of Windows so let's force it to the first date that always works.  If the 
    				// file reads that old then what's a few days for the sake of saving an assert.
    			year = 1970;
    			month = 1;
    			day = 3;
    		}
    		if(year < 1970) {
    			if(year < 70 && year > 38)
    				year += 2000;
    			else if(year > 70 && year < 1900)
    				year += 1900;
    			else
    				year = cTime.GetYear();
    		}
    		// Fix for GMT of 0 where MSFT timezone check subtracts 3600 seconds.  This will 
    		// keep the CTime function from claiming an invalid parameter.  Bug is actually
    		// in mktime()
    		if(year == 1970 && month == 1 && day == 1)
    			day = 3;
    		cTime = CTime(year, month, day, hours, minutes, seconds); 
    	}
    	return cTime;
    }

    Tom

  • 2012年3月15日 17:18
     
     建议的答复

    You can use basic time functions:t ime, _time32, _time64 and  asctime_s

    described in the link:http://msdn.microsoft.com/en-us/library/1f4c8f33(v=vs.80).aspx and http://msdn.microsoft.com/fr-fr/library/b6htak9c(v=vs.80).aspx


    Delphine GARRO

  • 2012年3月15日 22:07
     
     

    I was thinking.

    since the date would be manually entered by the user, using cin.getline.  Once entered I would then I would store it in an Array.

    I was going to make a function full of "if" statements calling the first two elements in the array and if they == 12 etc.

    it would cout " December" . Not sure how to integrate the day and year.

  • 2012年3月15日 23:22
     
     

    you can use strptime function for this.

    Thans


    Rupesh Shukla

  • 2012年3月16日 4:58
     
      包含代码
    Here is the code I have and it works. I would like to put the "If" statements in their own function and call it in the main. Have been unsuccessful thus far.
    # include <string>
    #include <iostream>
    using namespace std;
    int main()
    {
    string date;
    string Month;
    string Day;
    string Year;
      cout << ("Please enter a date (format: mm/dd/yyyy): ");
      cin >> date;
       
      Month = date.substr(0,2);
      Day = date.substr(3,2);
      Year = date.substr(6,4);
        if (Month == "01")
        {
          cout << "January " << Day << ","<< Year<<endl;          
        }
        else if(Month == "02")
        {
          cout << "February "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "03")
        {
          cout << "March "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "04")
        {
          cout << "April "<< Day << " "<< Year<<endl;           
        }
        else if(Month == "05")
        {
          cout << "May "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "06")
        {
          cout << "June "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "07")
        {
          cout << "July "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "08")
        {
          cout << "August "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "09")
        {
          cout << "September "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "10")
        {
          cout << "October "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "11")
        {
          cout << "November "<< Day << ", "<<Year<<endl;           
        }
        else if(Month == "12")
        {
          cout << "December "<< Day << ", "<< Year<<endl;           
        }
        else
        {
    		cout << "Idiot you enter the date incorrectly "<<endl;
    	}
            
        
    }

  • 2012年3月16日 5:11
     
     
    >I would like to put the "If" statements in their own
    >function and call it in the main. Have been unsuccessful
    >thus far.

    That should be very simple. What seems to be the problem
    you're having? Show the code you've tried.

    - Wayne
  • 2012年3月16日 15:00
     
     
  • 2012年3月16日 20:27
     
      包含代码

    Wayne

    This is what I attempted to do. It will compile but any date prints the else statement.

    # include <string>
    #include <iostream>
    using namespace std;
    void GetMonth(string & );
    void GetDay(string &);
    void GetYear(string &);
    int main()
    {
    string date;
    string Month;
    string Day;
    string Year;
      cout << ("Please enter a date (format: mm/dd/yyyy): ");
      cin >> date;
       
      GetMonth(date);
      GetDay(date);
      GetYear(date);
        if (Month == "01")
        {
          cout << "January " << Day << ","<< Year<<endl;          
        }
        else if(Month == "02")
        {
          cout << "February "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "03")
        {
          cout << "March "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "04")
        {
          cout << "April "<< Day << " "<< Year<<endl;           
        }
        else if(Month == "05")
        {
          cout << "May "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "06")
        {
          cout << "June "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "07")
        {
          cout << "July "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "08")
        {
          cout << "August "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "09")
        {
          cout << "September "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "10")
        {
          cout << "October "<< Day << ", "<< Year<<endl;           
        }
        else if(Month == "11")
        {
          cout << "November "<< Day << ", "<<Year<<endl;           
        }
        else if(Month == "12")
        {
          cout << "December "<< Day << ", "<< Year<<endl;           
        }
        else
        {
    		cout << "Idiot you enter the date incorrectly "<<endl;
    	}
            
        return 0;
    }
    void GetMonth(string &date)
    { 
    	string Month;
    	
      Month =  date.substr(0,2);
      
    	
    }
    void GetDay(string &date)
    {
    	string Day;
    	
        Day = date.substr(3,2);
    }
    void GetYear(string &date )
    {
    	string Year;
    	
        Year = date.substr(6,4);
    }

  • 2012年3月16日 20:42
     
     

    void GetMonth(string &date)
    {
      string Month;

      Month =  date.substr(0,2);
      
    }

    Ok, so you computed the value of Month. But you don't do anything with it. Remember, this is a variable that is local to the function only. Did you intend to return the value to the caller, perhaps?
  • 2012年3月17日 11:04
     
     

    Apparently I misunderstood the assignment. The original code worked but not the way the assignment wanted.

    I have to make an array and fill it with months ie "January", "Febuary", etc.  and use the numeric representation of the month as the index. Then I need to convert the numerical string to a int (probably using atoi). 


    Then  read in the data in main in the form of mm/dd/YYYY and pass it to a function that determines the month and returns a 

    new string with the month as a word…..

    Not sure exactly how to due this.

  • 2012年3月17日 13:05
     
     

    I have to make an array and fill it with months ie "January", "Febuary", etc.  and use the numeric representation of the month as the index. Then I need to convert the numerical string to a int (probably using atoi). 


    Then  read in the data in main in the form of mm/dd/YYYY and pass it to a function that determines the month and returns a 

    new string with the month as a word…..

    Not sure exactly how to due this.

    Do you ever pay attention to what others try to suggest to you? Two days ago i gave you a link from where you could borrow the fast and effective (though, rather expensive) way to translate date from string (such as "30/01/2012") into int format using std::istringstreamhttp://stackoverflow.com/questions/3263661/format-a-date-in-c.


  • 2012年3月17日 17:09
     
     

    Thanks for the reply Sergey

    I did read your earlier post. However we are not at that point in class so I can't use some of that code.  I  know most of the code I need to use I am not sure of how to actually impliment it or how I pass the mm/dd/yyyy to the function  or whether I do the conversion in the main and how I return the new string as month being a word. 

  • 2012年3月17日 17:32
     
     建议的答复
    >we are not at that point in class so I can't use some
    >of that code.

    This is *one* of the problems with asking for help with
    course assignments in a public forum. We have no way of
    knowing what has or hasn't been covered in the course.

    Even if we did, it's *your* assignment so you need to do it
    yourself. If the teacher wanted a collaborative or group
    effort then everyone in the course could sit down together
    and create a single program to be submitted by every student.
    While that might make the teacher's job easier - having only
    one program to grade - it would not be a very good way to
    teach students how to program by themselves.

    >I know most of the code I need to use I am not sure of how to
    >actually impliment it

    Then start coding - you'll learn from your mistakes what
    will or won't work. As you refine your approach the task
    should become clearer.

    >or how I pass the mm/dd/yyyy to the function  

    Any way you want. You obviously already know how to pass a
    std::string to a function, since you did it in your earlier
    code.

    >or whether I do the conversion in the main

    That's a design decision which you as the programmer must
    make yourself.

    >and how I return the new string as month being a word.

    You should already know how to return a value from a
    function. You also should already know how to pass more
    than one argument to a function, including pointers and
    references.

    Break the program down into discrete parts which can be
    coded as more or less independent sub-tasks. Then focus
    on coding one part of the program (a sub-task) and getting
    it to work. Eventually you will have all of the separate
    tasks coded and you can then cobble them together to get
    the final complete program. Think modularly rather than
    trying to construct the entire program in your mind
    before writing any code.

    - Wayne

  • 2012年3月17日 17:39
     
     

    Thanks for the reply Sergey

    I did read your earlier post. However we are not at that point in class so I can't use some of that code.  I  know most of the code I need to use I am not sure of how to actually impliment it or how I pass the mm/dd/yyyy to the function  or whether I do the conversion in the main and how I return the new string as month being a word. 

    I am sorry, but we can not do your work for you. We can only help you in times you get stuck with code. By the way, you also could mention what language features you already know and can implement in code. For example, do you code with classes, STL, in C, in C++, mixed C/C++, etc? It is for us to know what solutions we can propose you to implement as there are usually many ways to solve the problem.  
    • 已标记为答案 eddie174 2012年3月18日 2:48
    • 取消答案标记 eddie174 2012年3月18日 7:28
    •  
  • 2012年3月18日 2:48
     
     

    Thanks

    I followed your s an Waynes advice I have come up with a working program just need to smooth it.

  • 2012年3月18日 7:32
     
     已答复 包含代码

    Here is my new code it worked at first ran it a third time and it crashed and still crashes.

    Any suggestions??

    # include <iostream>
    # include <cstdlib>
    # include <string>
    using namespace std;
    //function prototype
    void DatePrint(const char *Date);
    int main()
    {
    	
    	char Date[20];
    	cout<<"Enter a date in the following format mm/dd/yyyy: ";
    	cin>>Date;
    	DatePrint(Date);
    	cout<<endl;
    	return 0;
    }
    void DatePrint(const char *Date)
    {
    	int month;
    	int day;
    	int year;
    	char mm[3];
    	char dd[3];
    	char *MonthStr[]={"January", "Febuary", "March",
    		               "April", "May", "June", "July",
    					   "September", "October", "November",
    					   "December"};
    	// input array to separate function arrays
    	mm[0] = Date[0];
    	mm[1] = Date[1];
    	mm[2] = '\0';
    	dd[0] = Date[3];
    	dd[1] = Date[4];
    	dd[2] = '\0';
    	//int equivalent/conversion
    	 
    	month = atoi(mm);
    	day = atoi(dd);
    	year = atoi(Date + 6);
    	if (month > 12 || month < 1 || !day || !year) //validation of input
    	{
    		cout<<"Big dummy follow instructions\n";
    	}
    	else
    		cout<<MonthStr[month - 1]<<" "<<day<<", "<<year<<endl; 
    }

  • 2012年3月18日 8:16
     
     

    caught my error I forgot August in the months

    Eveyone thanks for your input..