C# String to DateTime - c#

DateTime frm_datestart = DateTime.Parse(dateStart.Text);
This line throws the error:
Exception Details:
System.FormatException: String was not
recognized as a valid DateTime.
Where the entered string is from Jquery-UI, examples:
09/29/2010
09/30/2010
Anyone know what the correct format should be? I'm suprised this isn't working :S

You can use an overloaded version of the DateTime.Parse() method which accepts a second DateTimeFormatInfo parameter.
System.Globalization.DateTimeFormatInfo dti = new System.Globalization.DateTimeFormatInfo();
dti.ShortDatePattern = "MM/dd/yyyy";
DateTime dt = DateTime.Parse(dateStart.Text, dti);

look for DateTime.ParseExact method.

val = dateStart.Text.ToString("yyyy-M-d HH:mm:ss");

Use DateTime.ParseExact to specify format like this: DateTime.Parse("dd/MM/yyyy", dateStart.Text, null)

The problem with DateTime.ParseExact() method suggested in previous answers is, it fails on some Cultures. So your application may fail to run correctly on certain Operating Systems.
If you are sure that dateStart.Text will always be in the same format (i.e. en-US), you may try passing appropriate CultureInfo as a second argument. For format "MM/dd/yyyy" use CultureInfo.InvariantCulture.

Related

System.Format exception in C#

objDR["sidate"].ToString() has the date in this format "01-17-78" where objDR is a datarow.
When i am doing the following :
Convert.ToDateTime(objDR["sidate"].ToString())
I am getting a System.Format exception.
What could possibly be causing this?
Edit:
The exception is not coming now.Refer Marked answer. Just one more question : the "MM-dd-yy" used in the middle is used for conversion of date from one format to other or it is used for some other purpose? Because i changed it to "M-d-yy" but the format did not change.
You have to provide dateformat.
Try this
DateTime dt = DateTime.ParseExact("01-17-78", "MM-dd-yy", CultureInfo.InvariantCulture);
The Convert.ToDateTime() method takes in consideration your current culture in order to use that method you must make sure that the format of the DateTime you are passing to the method is the same format as your current culture.
Otherwise, you can do the following:
var date = DateTime.Parse(objDR["sidate"].ToString(),CultureInfo.InvariantCulture);
Another solution is to use the DateTime.ParseExact() method:
DateTime dt = DateTime.ParseExact(objDR["sidate"].ToString(), "MM-dd-yy", CultureInfo.InvariantCulture);
Link to DateTime struct in MSDN.
Link to DateTime.Parse(String, IFormatProvider) method overload in MSDN.
Link to the Convert.ToDateTime() method in MSDN.
Link to the DateTime.ParseExact() method on MSDN.
The problem is your current culture. Convert.ToDateTime uses the current culture to determine the convert format see the remakrs.
But one solution is to use DateTime.Parse(objDR["sidate"].ToString(), CultureInfo.InvariantCulture) so the culture is programmer given.

Convert ToDateTime fail when publish to Webserver

I got this problem about Convert String to Datetime.
This is my code:
#if (Convert.ToDateTime(item.EXPIRED_DATE) <= DateTime.Now)
{
<td><span style="color:red;">#item.EXPIRED_DATE</span></td>
}
When I run it on my localhost, it worked. But when I publish it on Webserver, I got this Error Message:
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
And this is item.EXPIRED_DATE value
"31/01/2018"
Please help me!
You could make use of DateTime.ParseExact:
DateTime.ParseExact(item.EXPIRED_DATE, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Why you got the error you have mentioned?
As you could read here Convert.ToDateTime, since the value you pass is not null, would invoke DateTime.Parse and return its result. The DateTime.Parse as it is mentioned here (look at the question: Which method do I call?) parses a date and time string by using the conventions of the current culture. So your program current's cutlure is not has not a format for DateTime as the one you want to use. This is why you have to use DateTime.ParseExact method providing the correct format.

Convert to datetime from Oracle

i know there are a lot of similar questions, but I couldn't find what I was looking for.
Here is my oracle date:
string testdate= "2014-01-07 15:00:00.0000000";
And here is how I tried to convert to datetime:
DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)
This throws a format exception. Any ideas?
My quick test also throws the string not valid datetime exception. Quick test:
Console.WriteLine(DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture).ToShortDateString());
I'd start by trying to avoid getting it as a string in the first place. Make sure you're using the appropriate data type in Oracle, and you should be able to call GetDateTime on the appropriate DataReader (or whatever you're using).
If you must parse it as text, then you need to specify a format which matches the value - so use 7 fs instead of 3, given that your value has ".0000000" at the end.
DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fffffff",
CultureInfo.InvariantCulture)
But again, I'd strongly urge you to avoid having to deal with the value as text at all.
Why use ParseExact at all? Reqular Parse seems to work.
var dt = DateTime.Parse("2014-01-07 15:00:00.0000000", CultureInfo.InvariantCulture);
// Prints out 2014-01-07T15:00:00.0000000
Console.WriteLine(dt.ToString("o"));

DateTime c# parsing

I try to parse DateTime.TryParse("30-05-2010"), and it throws an exception because it accepts MMddyyyy, and I need ddMMyyyy format. how can I change TryParse format?
thanks,
Dani
You can use the DateTime.TryParseExact method instead which allows you to specify the exact format the string is in
If you're making that adjustment because of local usage, try this:
bool success = DateTime.TryParse("30-05-2010", out dt);
Console.Write(success); // false
// use French rules...
success = DateTime.TryParse("30-05-2010", new CultureInfo("fr-FR"),
System.Globalization.DateTimeStyles.AssumeLocal, out dt);
Console.Write(success); // true
maybe you can use the overload with the formatprovider.
DateTime.TryParse("30-05-2010", <IFormatProvider>)
not sure how to correctly implement it, cant test anything here, but here's more info about the iformatprovider:
http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

C# , How can i build a DateTime object from a datetime present in a string object?

in C# , How can i build a DateTime object from a datetime present in a string object ( String strDate="11/11/2009")?
Have a look at the DateTime.Parse or DateTime.ParseExact methods.
Simplest case:
var result = DateTime.Parse(str);
There's also DateTime.ParseExact which is also worth knowing about.
There's also TryParse and TryParseExact which do the same thing, but won't throw an exception if the date is not valid - will just return a bool.
Use DateTime.Parse
Using that string format will give you a lot of issues later, such as what is "03/04/2009"?
try using DateTime.ParseExact.

Categories