In my property below I m parsing string to datetime.
public virtual string StartTimeLocal
{
set { StartTime = DateTime.Parse(value).ToUTCDateTime(); }
}
Just checked in value I have 26/1/2014 02:17 PM
Can you please help me what wrong I m doing and how to correct it ?
DateTime.Parse
parses standart date and time formats .
Your string is not one of them.
You can use DateTime.TryParseExact
or DateTime.ParseExact
methods instead.
string s = "26/1/2014 02:17 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/M/yyyy hh:mm tt",
CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
else
{
//Your string is not a valid DateTime.
}
Your input is formatted using en-US culture settings, so you should either make sure your application runs on system with local culture set to en-US or specify culture explicitly:
public virtual string StartTimeLocal
{
set { StartTime = DateTime.Parse(value, CultureInfo.GetCultureInfo("en-US")).ToUTCDateTime(); }
}
Try the below:
CultureInfo provider = CultureInfo.InvariantCulture;
format = "dd/MM/yyyy hh:mm tt";
result = DateTime.ParseExact(dateString, format, provider);
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.