Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Datetime problem

Hi,
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

ASKER CERTIFIED SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apparently, subtracting two dates will give you TimeSpan object. You cannot use dates as integers. Dates maths is a completely separate thing.
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");
            Console.ReadLine();
        }
    }
}

Open in new window