Converting the format of dates

This is a discussion on Converting the format of dates within the C++ Programming forums, part of the General Programming Boards category; My program has a vector of 50 objects like this : Code: trans.push_back(Transaction(1365,"Norwich","20030901","1200",12,005,"Nobody present")); What I need to do is ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Question Converting the format of dates

    My program has a vector of 50 objects like this :

    Code:
    trans.push_back(Transaction(1365,"Norwich","20030901","1200",12,005,"Nobody present"));
    What I need to do is convert all the dates from their current format (eg 20030901) to the standard UK date format eg 01/09/2003). I also need all the times to include a colon (eg 12:00).

    How can I go about this?

    Thanks.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    hmmm, let me see..
    20030901/10000 = 2003, you get the year
    (20030901%10000)/100 = 9, you get the month (or the date??)
    20030901%100 = 1, you get the date (or the month )

    1200/100 = 12, you get the hour
    1200%100 = 0, you get the minute

    search the board about getting 01/09/2003 instead of 1/9/2003, same thing with the time

    EDIT: here you go
    Last edited by alphaoide; 10-13-2003 at 04:55 PM.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It looks like you are sending the date and time to the Transaction constructor as strings.

    Inside of that constructor, you could just use regular string manipulation functions to pick out the details and rearrange them into the member variables that store the string.

    For example:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string dateString = "20030901";
    
        if (dateString.length() < 8)
        {
            std::cerr << "oops, bad date string." << std::endl;
            return 1;
        }
        std::string yearString = dateString.substr(0, 4);
        std::string monthString = dateString.substr(4, 2);
        std::string dayString = dateString.substr(6, 2);
    
        std::cout << dayString << "/" << monthString << "/" << yearString << std::endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  2. converting real player format to mp3??
    By yakabod in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-18-2003, 06:38 PM
  3. converting format
    By merlin371 in forum C++ Programming
    Replies: 1
    Last Post: 08-21-2003, 03:54 PM
  4. converting huge #'s in string format to double.
    By ganonl in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2003, 09:41 AM
  5. Converting text to PDU format
    By skye in forum C Programming
    Replies: 3
    Last Post: 02-06-2002, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21