/
Program.cs
101 lines (81 loc) · 3.15 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace Chapter12
{
class Program
{
static void Main(string[] args)
{
DateTime currentDate = DateTime.Now;
/*Print_To_Console(currentDate.ToString());
Print_To_Console(currentDate.ToShortDateString());
int month = currentDate.Month;
Print_To_Console(String.Format("Current Month:{0}" ,month.ToString().PadLeft(2,'0')));
month = currentDate.AddMonths(-4).Month;
Print_To_Console(String.Format("Current Month {0} = {1}", -4, month.ToString().PadLeft(2,'0')));
*/
Print_To_Console("Enter Date of Birth(\"MM/dd/YYYY\")");
String dateOfBirthStr = Console.ReadLine();
DateTime dobDate = GetDate(dateOfBirthStr);
// if hashcode ==0 then is default date which is not valid date
if (dobDate.GetHashCode() != 0)
{
Print_To_Console(String.Format("Entered Date of Birth(locale format): {0}", dobDate.ToShortDateString()));
Print_To_Console(String.Format("Today's Date (locale format): {0}", DateTime.Now.ToShortDateString()));
bool val = Validate_Date(dobDate);
if (val)
{
System.TimeSpan ageSpan = DateTime.Now - dobDate;
DateTime Age = DateTime.MinValue + ageSpan;
int Years = Age.Year - 1;
int Months = Age.Month - 1;
if (Years != 0)
{
Print_To_Console(String.Format("Age in years {0} ", Years.ToString()));
}
else
{
Print_To_Console(String.Format("Age in Months {0} ", Months.ToString()));
}
}
else
{
Print_To_Console("Entered Date is today's date or future date");
}
}
else
{
Print_To_Console(String.Format("Entered date {0} is invalid", dateOfBirthStr));
}
Print_To_Console("Press any key to exit....");
Console.ReadKey();
}
private static bool Validate_Date(DateTime date)
{
int compValue = date.CompareTo(DateTime.Now);
// Print_To_Console(compValue.ToString());
return (compValue < 0) ? true : false;
}
private static DateTime GetDate(string dateOfBirth)
{
DateTime date;
if (DateTime.TryParseExact(dateOfBirth, "MM/dd/yyyy", null, DateTimeStyles.None, out date))
{
Print_To_Console("Entered valid date, proceeding to age calculation");
}
else
{
Print_To_Console("Entered invalid date");
}
return date;
}
private static void Print_To_Console(String value)
{
Console.WriteLine(value);
}
}
}