Learner
Learner

Reputation: 563

Cannot Implicitly convert type string to System.date.time but declare in datetime

I'm a begginer to C# & write some code.but getting error there. Error is : Cannot Implicitly convert type string to System.date.time but i see here i declare in string

  public partial class HDate
    {
        private string StartYear;

        public string StartYear1
        {
            get { return StartYear; }
            set { StartYear = value; }
        }
        private string EndYear;

        public string EndYear1
        {
            get { return EndYear; }
            set { EndYear = value; }
        }
        private string Year;

        public string Year1
        {
            get { return Year; }
            set { Year = value; }
        }
        private DateTime StartDate;

        public DateTime StartDate1
        {
            get { return StartDate; }
            set { StartDate = value; }
        }
        private DateTime EndDate;

        public DateTime EndDate1
        {
            get { return EndDate; }
            set { EndDate = value; }
        }
        private string StartMonth;

        public string StartMonth1
        {
            get { return StartMonth; }
            set { StartMonth = value; }
        }
        private string EndMonth;

        public string EndMonth1
        {
            get { return EndMonth; }
            set { EndMonth = value; }
        }
        private DateTime StartDay;

        public DateTime StartDay1
        {
            get { return StartDay; }
            set { StartDay = value; }
        }
        private DateTime EndDay;

        public DateTime EndDay1
        {
            get { return EndDay; }
            set { EndDay = value; }
        }


        public HDate() { }

        public HDate(){
        DateTime today = DateTime.Now;
        int year = today.Year;
        int month = today.Month;
        DateTime day = today;

        DateTime StartDate = (StartYear + StartMonth + StartDay);
        DateTime EndDate   = (EndYear + EndMonth + EndDay);

        if (month <= 6)
        {
            //string StartYear = Convert.ToString(year-1);
            string StartYear = (year - 1).ToString();
            string StartMonth = Convert.ToString(7);
            string EndYear = Convert.ToString(year);
            string EndMonth = Convert.ToString(6);
            string EndDay = Convert.ToString(30);
        }
        else if(month >= 7)

        {
            string StartYear =Convert.ToString(year);
            string StartMonth = Convert.ToString(7);
            string StartDay = Convert.ToString(1);
            string EndYear = Convert.ToString(year+1);
            string EndMonth = Convert.ToString(6);
            string EndDay = Convert.ToString(30);

        }
           return StartDate+';'+EndDate;
       }

those data return it to another function..

Upvotes: 0

Views: 4321

Answers (2)

Patrick
Patrick

Reputation: 5592

I would assume that these might not work:

DateTime StartDate = (StartYear + StartMonth + StartDay);
DateTime EndDate   = (EndYear + EndMonth + EndDay);

private string StartYear;
private string StartMonth;
private DateTime StartDay;

private string EndYear;
private string EndMonth;
private DateTime EndDay;

The only possible "addition" that you can do (unless you overload the operator) is with datetime and timespan. http://msdn.microsoft.com/en-us/library/system.datetime.op_addition(v=vs.90).aspx

Year and month are strings and the last is a datetime object. You need to cast them to int's and create a new datetime from that. Something like this:

DateTime StartDate = new DateTime(Int32.parse(StartYear), Int32.parse(StartMonth), StartDay.Day);

Upvotes: 0

Habib
Habib

Reputation: 223282

You have defined StartYear as private string field, same with StartMonth, In C# you can't do the following:

DateTime StartDate = (StartYear + StartMonth + StartDay)

StartDay is of type DateTime, I am not sure what exactly you want to do, it looks like you are trying to construct the Date from StartYear, StartMonth and StardDay.

If you can convert the StartDate, StartMonth and StartDay to integer then you can use the DateTime Constructore , DateTime(Year,month,day); something like following;

    int StartYear = 2012;
    int StartMonth = 06;
    int StartDay = 15;

    DateTime dt = new DateTime(StartYear, StartMonth, StartDay);

Upvotes: 3

Related Questions