Sami Parkar
Sami Parkar

Reputation: 29

cannot implicitly convert type string to system.datetime in c#

I am trying to convert dateTime into string format such as dd-MM-yy,but while trying to convert into string format i have get an compile time error.

public List<ExpertTrckerReportTO> SetFeatureEventDetails(DateTime d1, DateTime currentDate, string eventName, List<ExpertTrckerReportTO> lst)
{
    ExpertTrckerReportTO _expertTrckerReportTO = new ExpertTrckerReportTO();

    _expertTrckerReportTO.DaysUntilFutureEvent = (d1 - currentDate).Days;
    _expertTrckerReportTO.FutureEventName = eventName;
    string format = "dd-MM-yy";           
        _expertTrckerReportTO.FutureEventDate = d1.ToString(format,CultureInfo.InvariantCulture);           
    lst.Add(_expertTrckerReportTO);

    return lst;
} 

the below line through the error

_expertTrckerReportTO.FutureEventDate = d1.ToString(format,CultureInfo.InvariantCulture);

Can any one help me me with the syntax how to solve the error.

Upvotes: 0

Views: 5749

Answers (2)

Owns_Rekt
Owns_Rekt

Reputation: 164

Try

DateTime.ToString();

Hope It's the solution you hoped :)

Upvotes: 0

darkhac
darkhac

Reputation: 588

That happend because _expertTrckerReportTO.FutureEventDate is DateTime. You cannot implicitly convert d1.ToString(format,CultureInfo.InvariantCulture); (string) to DateTime

May be you want

_expertTrckerReportTO.FutureEventDate = d1

Sorry. Post as answer because I haven't enough raiting to comment

Upvotes: 3

Related Questions