My HiddenField have a value and I want parse it to DateTime format
In code behind
DateTime dateTime;
string start = startDate.Value.ToString();
dateTime = DateTime.Parse(start);
my string format: 03/29/2020 - 04/04/2020
but I keep getting this error:
String was not recognized as a valid DateTime.
Regarding your case, it seems that you are getting an incorrect value in your startDate
which could be: 03/29/2020 - 04/04/2020
. If that is the case, you can do something like:
using System;
public class Program
{
public static void Main()
{
DateTime dateTime;
DateTime dateTime1;
DateTime dateTime2;
string start = "03/29/2020";
string incorrectdate=@"03/29/2020 - 04/04/2020";
string[] parsed=incorrectdate.Split('-');
dateTime = DateTime.Parse(start);
dateTime1=DateTime.Parse(parsed[0]);
dateTime2=DateTime.Parse(parsed[1]);
Console.WriteLine(dateTime);
Console.WriteLine(dateTime1);
Console.WriteLine(dateTime2);
}
}
Output:
3/29/2020 12:00:00 AM
3/29/2020 12:00:00 AM
4/4/2020 12:00:00 AM
Working Example: https://dotnetfiddle.net/dby8qQ