简体   繁体   中英

Not able to Parse DataTime From String to DATETIME

I am trying to Convert Date from string "01-07-2015" . I have tried all method DateTime.Parse and DateTime.ParseExact and cultural Varaint too but I am not able to parse the datetime.

string dateTimeString = item.startDate; 
var dateTime = DateTime.Parse(item.startDate); 
DateTime edate = DateTime.Parse(item.endDate);
if (sdate <= datetime && datetime <= edate)
{
    periodList.Add(item.fsid);
}

Output I am getting is {01-01-0001 12:00:00:AM}

This should work:

var dateTime = "01-07-2015";
var date = DateTime.ParseExact(dateTime, "mm-dd-yyyy", CultureInfo.InvariantCulture);

demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM