Calculate Age from Personal Code

himlastar

Member
Joined
Mar 16, 2019
Messages
10
Programming Experience
Beginner
How can I Calculate Age from Personal Code?
If I have to Input
String Pers_Code;
For Example mine is 040498-#####
How I get my age and When is my Birthday?
 
Last edited:
I'd suggest that magic would be a good option. Seriously though, your question is pretty much meaningless. There is nothing inherent in that code you specified that would provide the information you want. Maybe you mean that you have a database that contains that information but, if that's the case, that's what you should say. It's not for us to guess that sort of thing.
 
String Pers_Code;
Console.Write("Input the Personal Code");
Pers_Code = Console.ReadLine();
lets say I input - 040498-12236
I somehow get the date from today its 29/03/2019
from pers code u can see im 04/04/1998
2019-1998 = 20
And it should output
Console.WriteLine(Age) which in this case is 20
And my Birth Day is on 04/04 thats 04 April
But I dont know how do I get 98 out of string, probably in some array way
I dont know... I need help with this!
 
The DateTime.ParseExact or DateTime.TryParseExact method will allow you to convert that String to a DateTime value, so you can do actual time-based calculations with it and DateTime.Today.
 
Can you write exact code?

Console.Write("Input the Personal Code");
Pers_Code = Console.ReadLine(); // 040498-45266
DateTime.ParseExact(???)
 
I could, but we don't even know that I need to yet. You haven't even tried yet, so you haven't failed. You're allowed to do some research and find out things for yourself. First, read the documentation for those methods I mentioned. VS has a Help menu and supports standard context-sensitive Help (F1 key) that you've made no effort to use and you can also search the web and find that same documentation and more besides. Don't just sit on your hands. Do what you can for yourself first, then ask for help when you actually get stuck. We'll still be here if and when that happens.
 
I came up with this, if I input 040498 it works(but displays time too), if 040498-12236 ( How I should Input this) it crash

string Pers_kods;
Console.WriteLine(date);
Console.Write("Personas kodu: ");
Pers_kods = Console.ReadLine();
Console.Write($"Personas kods: {Pers_kods} ");
DateTime dt = DateTime.ParseExact(Pers_kods, "dMyy", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Console.ReadLine();

Could I do that he only reads the first 6 numbers from string?
 
Last edited:
Firstly, if you are specifically entering the date as a 6-digit string then why would you use a 4-character format string? Secondly, if the user enters a two-part code that is separated by a dash and you only want to parse the part before the dash then you need to get just that part and pass that to ParseExact. It's very easy to get just that part of the text and it's very easy to find out how to do that.

As for the issue, as the name suggests, a DateTime contains a date and a time. Internally, it is just a number that is an offset from a specific moment in time. If you want to represent just a date with a DateTime value then you simply set the time part to zero, i.e. midnight. When it comes to displaying a DateTime value, it's completely up to you what the output looks like. It needs to be converted to a String for display purposes so, if you want a specific format, you should convert it to a String yourself and specify the format you want, rather than leaving the conversion up to the system and assuming that it will magically know what format you want of the many possible choices. Again, it's very easy to find out how to convert a DateTime to a String in a specific format, so I'll leave that part to you.
 
Back
Top Bottom