輸入某人出生日期(以字符串方式輸入,如1987-4-1)使用DateTime和TimeSpan類,(1)計算其人的年齡;(2)計算從現在到其60周歲
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入出生年份");
string n = Console.ReadLine();
int n1;
int.TryParse(n, out n1);
Console.WriteLine("請輸入出生yue份");
string y = Console.ReadLine();
int y1;
int.TryParse(y, out y1);
Console.WriteLine("請輸入出生日子");
string r = Console.ReadLine();
int r1;
int.TryParse(r, out r1);
DateTime dt = new DateTime(n1,y1,r1);
Console.WriteLine(dt.ToShortDateString ());
int nl = DateTime.Now.Year - n1;
Console.WriteLine("他的年齡是{0}",nl);
DateTime dt1 = new DateTime(n1+60, y1, r1);
TimeSpan ts = dt1 - DateTime.Now;
Console.WriteLine("從現在到其60周歲期間,總共{0}天",ts.Days.ToString ());
}
}
}
分享到: