0

Problem: I have a table that list the person DOB.

I have a field (called Person_DOB) that I can use to grab the person DOB from the database. I would like to grab the person year they were born and compare it with the current year we are in.

Ex. 1988-09-26 00:00:00.000

I have a field that I can call that contains the DOB of the person. How can I covert that to a date and calculate their age using C#?

I have tried the following but it appears to not work:

<%
int now = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
int dob = int.Parse("Person_DOB".ToString("yyyyMMdd"));
int age = (now-dob) / 10000;
Response.Write(age);
%>

But I am getting invalid argument error. What am I doing wrong?

Roberto Flores
  • 775
  • 2
  • 12
  • 46
  • 1
    Paging @JonSkeet for a [NodaTime](http://nodatime.org/) plug – maccettura Jul 10 '17 at 16:33
  • 1
    The `"Person_DOB"` could not be convert to int. – Salah Akbari Jul 10 '17 at 16:34
  • It's not a direct duplicate because the issue it not only with DateTime. – Vlad Stryapko Jul 10 '17 at 16:36
  • @S.Akbari okay.... – Roberto Flores Jul 10 '17 at 16:38
  • `DateTime.Parse` will get you a date. See the duplicate question for how to calculate the age. – Rufus L Jul 10 '17 at 16:41
  • @VladStryapko what is the issue, then? If I understood correctly, OP is trying to convert `DateTime.Now` to string, then convert the date got from database to string, then convert both to integer and calculate age. This can be done in a simpler way, as showed in the other question. – Alisson Reinaldo Silva Jul 10 '17 at 16:41
  • @Alisson I have tried those approaches and it keeps erroring out – Roberto Flores Jul 10 '17 at 16:42
  • 1
    If you have a string date, you need to parse it to datetime first before you can tostring it to a date format. Probably include `var dateDob = DateTime.Parse(Person_DOB).ToString("yyyyMMdd"); `where Person_DOB has value similar to your example – Lawrence Jul 10 '17 at 16:45
  • @Alisson I think the question was mistakenly marked by L.B as a duplicate. Here's why: If we go to the 'duplicate' question, we see that it's about comparing two DateTime instances. However, OP's question is more general and even though it results in comparing two DateTime instances, OP cannot simply look up the answer. They will have to ask the next question - how to convert values in the database to DateTime. That's why I don't think it's a direct duplicate. It can be split into 2 question: 1. how to convert values to DateTime and 2. how to compare them, the latter being a 100% duplicate. – Vlad Stryapko Jul 10 '17 at 16:45
  • @maccettura - I already have [a NodaTime based answer](https://stackoverflow.com/a/20715576/634824) in the dup question linked. – Matt Johnson-Pint Jul 10 '17 at 16:46
  • @L.B why was it marked as a duplicate if OP clearly cannot find an answer in the 'duplicate' question without asking additional questions? It cannot be ' an exact duplicate of an existing question'. – Vlad Stryapko Jul 10 '17 at 16:48
  • @MattJohnson: do not really want to use that. I thought there was another approach – Roberto Flores Jul 10 '17 at 16:54
  • @Lawrence I have tried your approach but it keeps not liking it – Roberto Flores Jul 10 '17 at 17:00
  • @VladStryapko: Do you have any suggestion to achieve what I am looking for? If not, I will keep looking – Roberto Flores Jul 10 '17 at 17:03
  • `"Person_DOB".ToString("yyyyMMdd")` << as far as I know this can't work. It looks you are getting the person *DOB* as a string and trying to convert directly. You need something like `DateTime.Parse("Person_DOB").ToString("yyyyMMdd")`, which is what @Lawrence suggested. – Alisson Reinaldo Silva Jul 10 '17 at 17:04
  • @RobertoFlores The algorithm's pretty straightforward: 1. Get the DOB from the database 2. Get current DateTime 3. Calculate the age based on the way described in another question. If anything's not clear, ask a separate question about that. – Vlad Stryapko Jul 10 '17 at 17:10
  • @Alisson: Again , I have tried that on my end. Keeps not liking it – Roberto Flores Jul 10 '17 at 17:15
  • 1
    Yes, many of the answers in the dup question address how to do the math. The problem with your code is that `"Person_DOB"` is just a string, it's not pulling out the actual value out of any field. Assuming you're connecting to a database and have a datareader, it's going to be something like `reader["Person_DOB"]` – Matt Johnson-Pint Jul 10 '17 at 17:19
  • So it sounds like the issue you're having is converting the database value to a `DateTime`. There have been some good suggestions here in the comments about how to do that. At this point, I think you should delete this question, give one of the suggestions a try, and then ask a new question specifically about that, including the actual exception message and stack trace (not just *"it keeps not liking it"*). – Rufus L Jul 10 '17 at 17:41

0 Answers0