Datetime problem

How to correct errors

Cannot implicitly convert type 'System.TimeSpan' to 'int'
Operator '<' cannot be applied to operands of type 'System.DateTime' and 'int'

Open in new window

to these 2 lines

                DateTime dt0 = DateTime.Today;int age=birth_dt-dt0;
                if (dt0<18)

Open in new window

LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
Some minor mistake, I guess. May be you want to say

if(age < 18)

Open in new window

Also you havn't mentioned what birth_dt is. Even I am not sure if direct substraction of dates will give you years. Are you sure?

The best approach would be:

int age = dt0.year - birth_dt.year;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nitin SontakkeDeveloperCommented:
Apparently, subtracting two dates will give you TimeSpan object. You cannot use dates as integers. Dates maths is a completely separate thing.
David Johnson, CD, MVPOwnerCommented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ee_29075070
    class Program
        static void Main(string[] args)
            DateTime BirthDay, Today, Years;
            String Not = " Not ";
            BirthDay = Convert.ToDateTime("01/01/1912");
            Today = DateTime.Now;
            Years = BirthDay.AddYears(18);
            if (Today >= Years)
                Not = "";
            Console.WriteLine("User is " + Not + "18 Years Old");

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.