C++ 如何从一个整数值中获取三个独立的日期值(日、月、年)

C++ How to Get three separate date values (day, month, year) from one integer value

好吧,我要求用户提供 (yyyy/mm/dd) 格式的日期,然后在本例中归结为一个函数 dateSplit。我知道我怎么知道年份,因为我知道它永远是我可以取模的前四位数字。任何人都知道如何计算月份和日期,这里是我目前拥有的代码:

void dateSplit(int date, int& year, int& day, int& mon)
{
    // date % 10000 is a floating point value but i put it into an int to cut the back off
    date % 10000 = year;

}

有谁知道我怎么能只读中间两个数字和最后两个数字?

我想我会把我的主要代码放在这里,这样人们就可以看到全貌:

int main()
{
    // Variable Declarations
    string airportCode, lat, longitude, timeZone;
    int date;
    char contin = 'Y';
    while (contin == 'Y' || contin == 'y')
    {
        // Ask User for Airport Code
        cout << "Please Enter an Airport Code: ";
        cin >> airportCode;

        //Call to retrieve information
        retrieveFromFile(airportCode, lat, longitude, timeZone);

        //Call for date
        cout << endl << "Please Enter a date(yyyy/mm/dd): ";
        cin >> date;

        // Continue running program?
        cout << endl << "Would you like to continue? (Y/N): ";
        cin >> contin;

    }
}

这是最终实现的代码:

     void dateSplit(int date, int& year, int& day, int& mon)
    {
        // Ex. if date is 20150623, then it takes that number
        // moves the decimal place over four digits
        // then cuts off after the decimal point
        // leaving just the first four digits
        year = date / 10000;
        date %= 10000;
        mon = date / 100;
        day = date % 100;




        cout << endl << "Year: " << year
             << endl << "Day : " << day
             << endl << "Month:" << mon;

    }

只需使用除法和模数将其切碎,例如:

year = date % 10000;
date /= 10000;
month = date % 100;
day = date / 100;