Peter Chan
asked on
Datetime problem
Hi,
How to correct errors
How to correct errors
Cannot implicitly convert type 'System.TimeSpan' to 'int'
Operator '<' cannot be applied to operands of type 'System.DateTime' and 'int'
to these 2 lines DateTime dt0 = DateTime.Today;int age=birth_dt-dt0;
if (dt0<18)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
}
}