Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;获取用户选择的日期和实际日期之间的年份(计算天数、月份、年份)_C++_Datetime_Time_Date Difference - Fatal编程技术网

C++ c++;获取用户选择的日期和实际日期之间的年份(计算天数、月份、年份)

C++ c++;获取用户选择的日期和实际日期之间的年份(计算天数、月份、年份),c++,datetime,time,date-difference,C++,Datetime,Time,Date Difference,我试着这样做: struct Den_t { int day, month, year; }; int main() { struct Den_t* Datum = new struct Den_t; struct Den_t* Dnes = new struct Den_t; time_t theTime = time(NULL); struct tm aTime; localtime_s(&aTime, &

我试着这样做:

struct Den_t
{
    int day, month, year;
}; 

int main()
{
        struct Den_t* Datum = new struct Den_t;
        struct Den_t* Dnes = new struct Den_t;

    time_t theTime = time(NULL);
    struct tm aTime;
    localtime_s(&aTime, &theTime);

    Dnes->day = aTime.tm_mday;
    Dnes->month = aTime.tm_mon + 1;
    Dnes->year = aTime.tm_yday + 1900;

    cin >> Datum->day >> Datum->month >> Datum->year;
    if (Dnes->year - Datum->year >= 18 )
        cout << "full aged " << endl;
    else
        cout << "not full aged " << endl;
    system("PAUSE");
    return 0;
}
struct Den\t
{
整数日、月、年;
}; 
int main()
{
结构面*基准面=新结构面;
struct Den_t*Dnes=新结构Den_t;
时间=时间(空);
结构时间;
本地时间(时间和时间);
Dnes->day=aTime.tm\u mday;
Dnes->month=aTime.tm_mon+1;
Dnes->year=aTime.tm_yday+1900;
cin>>基准->日>>基准->月>>基准->年;
如果(Dnes->年份-基准->年份>=18)

这里的代码逻辑有问题。 例如:

Datum is 31/12/1982
Dnes is 01/01/2000
年差为18岁,但年龄为17岁和2天

考虑使用标准库函数,而不是重新发明轮子。 例如,这可能是有用的

这是一个非常肮脏的例子,但它可以做到:

   time_t dnes;
   time(&dnes);

   // Set datum here ... 
   cin >> Datum->day >> Datum->month >> Datum->year;
   datum.tm_mday = Datum->day;
   datum.tm_mon =  Datum->month - 1;
   datum.tm_yday = Datum->year - 1900;

   datum->tm_yday+=18;

   if (difftime(dnes, mktime(&datum)) <0 )
        cout << "not full aged " << endl;
    else
        cout << "full aged " << endl;
时间数据;
时间(&dnes);
//在这里设置基准。。。
cin>>基准->日>>基准->月>>基准->年;
datum.tmmday=基准->天;
datum.tm_mon=datum->month-1;
datum.tm_yday=基准->年份-1900;
基准->tm_yday+=18;
如果(difftime(dnes、mktime和datum))使用这些库:

这就是我解决问题的方法。首先是代码,然后是解释:

#include "tz.h"
#include "date.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    std::cout << "Enter birthday [day month year]:";
    int di, mi, yi;
    std::cin >> di >> mi >> yi;
    if (std::cin.fail())
    {
        std::cout << "Invalid date\n";
        return 1;
    }
    auto y = year{yi};
    if (!y.ok())
    {
        std::cout << "Invalid year\n";
        return 1;
    }
    auto m = month(mi);
    if (!m.ok())
    {
        std::cout << "Invalid month\n";
        return 1;
    }
    auto d = day(di);
    if (!d.ok())
    {
        std::cout << "Invalid day\n";
        return 1;
    }
    auto birthday = y/m/d;
    if (!birthday.ok())
    {
        std::cout << "Invalid birthday\n";
        return 1;
    }
    auto local_time = current_zone()->to_local(system_clock::now());
    auto today = year_month_day{floor<days>(local_time)};
    auto age = today.year() - birthday.year();
    if (birthday + age > today)
        --age;
    if (age >= years{18})
        std::cout << "full aged at " << age.count() << "\n";
    else
        std::cout << "not full aged at " << age.count() << "\n";
}
获取本地时间

此本地时间可转换为本地年、月和日,具体如下:

auto today = year_month_day{floor<days>(local_time)};
今天的年份和生日之间的差异是年龄的第一个近似值。通过计算你今年生日的日期,可以对这个近似值进行细化。如果今年的生日仍然是未来的生日,那么按照惯例,我们会将其视为年轻一岁。如果我们做的事情不太倾向于一条腿我们可以通过其他方式进行计算,比如四舍五入到最近的年份(使用这个库也很容易)

如果生日在2月29日,上述代码仍然有效:
生日+年龄将产生(75%的几率)在无效日期中,例如:2015年2月29日。但是,此无效日期将正确地比较大于2015年2月28日和小于2015年3月1日,正如我们所需要的那样!无效日期是你的朋友,而不是你的敌人——你只需知道它们的存在以及如何处理它们

现在,报告您的发现是一件简单的事情:

if (age >= years{18})
    std::cout << "full aged at " << age.count() << "\n";
else
    std::cout << "not full aged at " << age.count() << "\n";
if(年龄>=岁{18})

std::不能实现自己的日期时间函数(除非是学校作业或类似作业)但是,如果你想知道一个人的确切年龄,你也必须考虑出生日期。例如,如果假定18岁的人在1月1日或12月31日出生,那就很重要了。@ BPrasson就是我所做的,我用CIN把日期输入为“日-月年”(空间分开)。但我不知道在mycode cin之后现在如何继续:时间(&dnes);//在这里设置datum…aTime.tm_yday+=18;您希望它返回什么?您的代码打印到输出并返回0。您在哪里看到随机数?您需要自己实现读取
数据的部分。您已经为您的数据类型这样做了,只需将输入放入
datum
结构中即可,我输入了3121982,它总是返回其他的数字。我只想得到这两个日期之间的年龄差异,比如今天和用户使用cin输入的日期,如上面的示例所述:SY我们的示例检查两个日期之间是否至少有整整18年的时间。
auto age = today.year() - birthday.year();
if (birthday + age > today)
    --age;
if (age >= years{18})
    std::cout << "full aged at " << age.count() << "\n";
else
    std::cout << "not full aged at " << age.count() << "\n";