0

I want to calculate the ones year of birth by taking his current age, for eg his age is 30, in datetimepicker box the date should be displayed as 01/01/1982. It should be auto calculation.

         int todayyears = int.Parse (DateTime.Now.Year.ToString());

        int a = int.Parse (int.Parse(txt_age.Text).ToString());

        string dif = (todayyears - a).ToString();

        txt_dob.Text = dif;

Here txt_age is input value given, txt_dob is diaplayed the date of birth automatically after the input given in txt_age.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • 1
    Why would you convert DateTime.Now.Year to a string and then parse it again? – Jon Skeet Mar 08 '12 at 09:37
  • and why convert txt_age.Text to int to string and to int again? :) – arunes Mar 08 '12 at 09:38
  • Why are you converting int to string then back to int ( variable "a") – IAmGroot Mar 08 '12 at 09:38
  • @JonSkeet, this really should be a compilation error (like trying to read an unassigned location). – Adrian Iftode Mar 08 '12 at 09:38
  • @AdrianIftode: Why would that possibly fail to compile? You shouldn't (and can't) expect the compiler to spot *all* dumb code. The expression `DateTime.Now.Year.ToString()` is a perfectly valid one. The call to `int.Parse` with a string argument is entirely reasonable. If you want this to fail to compile, you should think about exactly where you would change the C# specification - what rule would you add? – Jon Skeet Mar 08 '12 at 09:41
  • @JonSkeet I saw this in so many legacy projects on I worked, I'm just tired to correct this. – Adrian Iftode Mar 08 '12 at 09:44
  • well just print `"01-01-"` + (DateTime.Now.Year - parsedAge).ToString()` :) – V4Vendetta Mar 08 '12 at 09:46
  • 2
    This is *not* a duplicate of the given question: it's the reverse. The supposed duplicate starts with a DateTime, and tries to work out the age. This question *starts* with the age, and works out the start of the year containing their birthday. – Jon Skeet Mar 08 '12 at 09:48
  • Well the question was more to arrive at the year of birth which is available in the so marked duplicate ! – V4Vendetta Mar 08 '12 at 09:48
  • @V4Vendetta: In the supposed duplicate we **start** with that year as part of the user input... so of course it's available. – Jon Skeet Mar 08 '12 at 09:49
  • 1
    @JonSkeet indeed which is not the case here, voted for reopen – V4Vendetta Mar 08 '12 at 09:51

5 Answers5

6

It looks like you're converting way too many things to strings, for no reason.

If you always want to display January 1st, you just need:

// Note: use int.TryParse to avoid exceptions if the user input isn't an integer
int age = int.Parse(txt_age.Text);

DateTime date = new DateTime(DateTime.Today.Year - age, 1, 1);

txt_dob.Text = date.ToString(); // Or use a specific format

Note that "dob" in "txt_dob" is inappropriate, as this isn't the user's date of birth, just January 1st in their year of birth which is very different.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Should be something like that:

txt_dob.Text = DateTime.Now.AddYears(int.Parse(txt_age.Text) * -1).ToString("01/01/yyyy");
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
2

You can create a datetime with the given year.

So calculate the year. ( Current year - Age in Years).

int Age = 30; // obtain from your given field.
int YofBirth = DateTime.Now.Year - Age;

DateTime d = new DateTime(YofBirth , 1, 1);

Then use this Datetime d to set the field.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
1

Try this

int todayyears = DateTime.Now.Year;
            int a = int.Parse(txt_age.Text);
            string dob = new DateTime(todayyears - a, DateTime.Today.Month, DateTime.Today.Day);
txt_dob.Text=dob;
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
0

Something like this:

int age;
if(int.TryParse(txt_age.Text,out age))
{
    txt_dob.Text= new DateTime(DateTime.Now.Year-age,1,1).ToShortDateString();
}
Arion
  • 31,011
  • 10
  • 70
  • 88