【问题标题】:c++ get years between date chosen by user and actual date(counting days,months,years)c ++获取用户选择的日期和实际日期之间的年数(计算天数、月数、年数)
【发布时间】:2015-10-22 14:04:02
【问题描述】:

我试过这样做:

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 比较用户输入的实际时间和日期的年月日 程序?

【问题讨论】:

  • 不要实现你自己的日期时间函数(除非是学校作业或类似的),而是使用functionality in the standard library
  • 你有一个好的开始,但如果你想知道一个人的确切年龄,你还必须考虑出生日期。例如,假设 18 岁的孩子出生于 1 月 1 日或 12 月 31 日,这很重要。
  • @BoPersson 这正是我所做的,我使用 cin 将日期输入为“日月年”(每个空格分开)但我现在不知道如何继续

标签: c++ datetime time date-difference


【解决方案1】:

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

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

年差是18岁,年龄是17零2天。

考虑使用标准库函数,而不是重新发明轮子。 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;

【讨论】:

  • 在 mycode cin: time_t dnes 之后;时间(&dnes); // 在此处设置数据 ... aTime.tm_yday += 18; cout
  • 你希望它返回什么?您的代码打印到输出并返回 0。您在哪里看到随机数?
  • 需要自己实现读取datum的部分。您已经为您的数据类型这样做了,只需将输入放入 datum 结构中。
  • 好吧,我输入 31 12 1982,它总是返回其他数字。我只想得到年龄差异,在这两个日期之间,如今天和用户使用 cin 输入的日期,如上面的示例中所述:S
  • 您的示例检查两个日期之间是否至少相隔 18 年。
【解决方案2】:

使用这些库:

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

我会先去检查用户输入的有效性。我下面的内容似乎是最低限度的:

  • 必须是整数输入。
  • 每个整数输入必须有一个合理的值(例如,月份必须在 [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";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 2023-01-31
    • 2021-07-08
    • 1970-01-01
    • 2019-05-02
    • 2018-08-14
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多