将出生年月换算成年龄的函数是什么

来源:百度知道 编辑:UC知道 时间:2024/07/08 16:41:22

没有直接可用的函数,你可以用当前的日期,减去你的出生日期,再除以365(366)得到近似的年龄数,你也可以自己编写一个函数

出生年月格式:1968-08-18 或1968年8月
=DATEDIF(C3,TODAY(),"y")

不知道你懂什么语言,我就用 C++ 主要部分给你写了一下。也没检查。大概就是这个样子的。
#include<iostream.h>

class Date{
public:
int year;
int month;
int day;
};

int main()
{
Date now,birth;
//这里自己写个输入now和birth的代码;
int myAge = age(now,birth);
if(myAge == -1)
{
cout<<"Happy Birthday!"<<endl;
}else{
cout<<"your age = "<<myAge<<endl;
}
return 0;
}

int age(now,birth)
{
int i_age;
i_age = now.year - birth year; //初始化你的年龄
if(now.month < birth.month)
{
i_age --;
}
else if(now.month == birth.month)
{
if(now.day < birth.day)