【C#】小ネタ 誕生日を計算する
今日は調子がいい!ってことで2連ちゃん!
なお、珍しくコンソールアプリケーションなんて使ってみてますよー
誕生日を基に年齢を計算します。
参考URL↓
http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c
class Program { static void Main(string[] args) { var birthDay = DateTime.ParseExact("1987/01/29 00:00:00", "yyyy/MM/dd HH:mm:ss", null); Console.WriteLine(GetAge(birthDay)); Console.ReadLine(); } public static int GetAge(DateTime birthDay) { var now = DateTime.Now; return ((now.Year * 10000 + now.Month * 100 + now.Day) - (birthDay.Year * 10000 + birthDay.Month * 100 + birthDay.Day)) / 10000; } }
文字列からフォーマットを指定してDateTimeを初期化なんてこともやってますねー
あ、ちなみに1987/01/29は私の誕生日なので覚えて帰ってくださいねー
さー仕事しよ!