c++获取用户选择的日期与实际日期之间的年份(计算日、月、年)

c++ get years between date chosen by user and actual date(counting days,months,years)

本文关键字:日期 计算 用户 获取 选择 c++ 之间      更新时间:2023-10-16

我试过了:

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;
}

但是我不明白我应该怎么比较和减少,有人能解释一下吗

我还需要做什么来告诉人们的日期,例如float by比较年、月、日的实际时间和用户输入的日期这个项目吗?

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

Datum is 31/12/1982
Dnes is 01/01/2000

年龄相差18岁,年龄相差17天。

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

这是一个非常肮脏的例子,但它会完成工作:

   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;

使用这些库:http://howardhinnant.github.io/date/date.html

http://howardhinnant.github.io/date/tz.html

这就是我将如何处理这个问题。首先是代码,然后是解释:

#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 daten";
        return 1;
    }
    auto y = year{yi};
    if (!y.ok())
    {
        std::cout << "Invalid yearn";
        return 1;
    }
    auto m = month(mi);
    if (!m.ok())
    {
        std::cout << "Invalid monthn";
        return 1;
    }
    auto d = day(di);
    if (!d.ok())
    {
        std::cout << "Invalid dayn";
        return 1;
    }
    auto birthday = y/m/d;
    if (!birthday.ok())
    {
        std::cout << "Invalid birthdayn";
        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";
}

我会先费点劲来检查用户输入的有效性。下面的内容看起来像是最小值:

  • 必须为整型输入。
  • 每个积分输入必须有一个合理的值(例如month必须在[1,12]范围内)。
  • y/m/d必须是有效的日期。

一个更健壮的程序可能会给用户输入的内容一些反馈,并给他另一个改正错误的机会。

一旦确定我们有一个有效的生日,我们需要在当地时区获得当前日期。:

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;

今天的年龄和生日的年龄之差是年龄的近似。通过计算今年您的生日是的日期,可以改进这个近似。如果今年的生日还在未来,那么按照习俗,我们将其计算为年轻一岁。如果我们做的事情不太倾向于法律体系,而更倾向于科学工作,我们可以用其他方法进行计算,比如四舍五入到最近的年份(在这个库中也很容易做到)。

如果生日是2月29日,上述代码仍然有效:birthday + age将(75%的可能性)导致无效日期,例如:Feb/29/2015。然而,这个无效的日期将正确地比较大于feb/28/2015和小于mar/1/2015,正如我们所需要的那样!无效日期是你的朋友,而不是你的敌人——你只需要知道它们的存在,并知道如何处理它们。

现在报告你的发现很简单:

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