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;
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 查询SQLite数据库中的日期
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 用C++在单独的头文件中完成函数体
- 类模板的成员功能的定义在单独的TU中完全专业化
- 如何使用单独文件中的派生类访问友元函数对象
- CoInitialize()在单独的线程上崩溃而不返回
- Log4cpp:以UTC/GMT时区打印日期
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- 使用C++的日期库读取时间
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 日期格式为C++
- 单独定义模板化嵌套类方法的正确语法
- 如何将 2 个类分成单独的 .h 文件并正确设置它们
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 如何在 C++20 计时中为日期添加天数?
- 以mm/dd/yyyy格式输入日期,并将其拆分为3个单独的变量
- C++ 如何从一个整数值中获取三个单独的日期值(日、月、年)
- 如何使用速推库比较两个单独的日期
- 如何在两个提升时间之间获取每个单独的日期