How to Convert string to date in c# [duplicate] - c#

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 8 years ago.
i have string format of date looks like "04/16/2014 19:10", i want to convert it to DateTime.
i tried, below codes, but it didn't work. i got error like "String was not recognized as a valid DateTime."
How to convert to datetime
DateTime dt1 = DateTime.Parse(DateTimeString);
DateTime dt = System.Convert.ToDateTime(DateTimeString);

The problem is Parse, as you are using it, will take into account the current culture of the machine which means (depending on where you are) that date could be interpreted differently.
Whenever you are parsing specific dates you should use ParseExact or TryParseExact, that way you leave no room for ambiguity on how the date should be interpreted (regardless of culture)
DateTime dt;
if (DateTime.TryParseExact("04/16/2014 19:10", "MM/dd/yyyy hh:mm",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
// date was parsed correctly, use `dt`
}

You may want to use ParseExact and specify the format yourself:
DateTime d = DateTime.ParseExact("04/16/2014 19:10", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);

Related

DateTime.ParseExact Giving Un Expected Error [duplicate]

This question already has answers here:
Why can't DateTime.ParseExact() parse "9/1/2009" using "M/d/yyyy"
(7 answers)
Closed 4 years ago.
I have some problem for which I am unable to find the solution.
Here is my code
string DateString = System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
string Format = "dd/MM/yyyy hh:mm:ss tt";
DateTime DT = DateTime.ParseExact(DateString, Format, CultureInfo.InvariantCulture);
string DTE = DT.ToString("dd/MM/yyyy");
The code is extracting an Error which I am pasting below. Please tell me what exactly is the problem (My System Date and Time has been set to 3/29/2018 like this and I don't want to change it at all) here is the error
Error Picture
During DateTime conversion to string, you should use CultureInfo.InvariantCulture also. If you do not this, DateString is converted as your machine culture, but you convert back again culture invariant. This may leads problem.
For example: when I use your code, DateString has "OS" value agains to tt field, after than I try to convert into DateTime with CultureInvariant, OS cannot be solved.
string DateString = System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string Format = "dd/MM/yyyy hh:mm:ss tt";
DateTime DT = DateTime.ParseExact(DateString, Format, CultureInfo.InvariantCulture);
string DTE = DT.ToString("dd/MM/yyyy");

How to convert dd/MM/YYYY formatted string date to YYYY-MM-dd datetime? [duplicate]

This question already has answers here:
Converting string format to datetime in mm/dd/yyyy
(6 answers)
Closed 6 years ago.
I wanna convert dd/MM/YYYY formatted string date to YYYY-MM-dd datetime. But it returns to me
"String was not recognized as a valid DateTime."
How can i convert from "04/26/2016" string to yyyy-MM-dd datetime format?
DateTime dt = DateTime.ParseExact("04/26/2016", "yyyy-MM-dd", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
You parse the string with date in wrong way.
You should:
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("yyyy-MM-dd"));
Technically, you can do just some string manipulations:
String source = "04/26/2016";
String result = String.Join("-", source.Split('/').Reverse());
But, DateTime.ParseExact is a better solution:
String result = DateTime
.ParseExact(source, "MM/dd/yyyy", CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Clearly, your format and string does not exactly match. From documentation;
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
You should use MM/dd/yyyy format instead.
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
And if you wanna get it's string representation with yyyy-MM-dd format, just use ToString method like;
Console.WriteLine(dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
Be aware, there is no YYYY as a custom date format. Since those specifiers are case sensitive, you should use yyyy format specifier instead.
Try this way
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("yyyy-MM-dd"));

Convert string("2015-03-24T12:31:33.8700000") to c# datetime [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I need to convert a string with date to valid c# date time object.
input string example = "2015-03-24T12:31:33.8700000"
output c# datetime
I tried doing this
DateTime.ParseExact(x.claimDetails.clmDOA, "yyyy-MM-dd HHmmss", CultureInfo.InvariantCulture)
Buit it gave exception
String was not recognized as a valid DateTime
Please Note: I googled thoroghly . Somehow i couldnt find any string
with "T" included as in datetime string.
Prior to downvoting if one can suggest me exactly where a question is answered with datetime string containg a T in it or of this format.yyyy-MM-dd'T'hh:mm:ss.fffffff
DateTime dt = DateTime.Parse(example);
DateTime dt = DateTime.ParseExact(example, "yyyy-MM-dd'T'hh:mm:ss.fffffff", CultureInfo.InvariantCulture);
Should work for you.
From DateTime.ParseExact documentation;
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
In your case, they are not.
You need to use T as a string literal delimiter, specify : as a TimeSeparator and seconds fraction (with fffffff specifier) as well.
var dt = DateTime.ParseExact("2015-03-24T12:31:33.8700000",
"yyyy-MM-dd'T'HH:mm:ss.fffffff",
CultureInfo.InvariantCulture);
DateTime.ParseExact(example , "yyyy MM dd HH:mm:ss", CultureInfo.InvariantCulture);

DateTime.ParseExact() - DateTime pattern 'y' appears more than once with different values

I have spent a day trying to get DateTime.ParseExact() to work based on this correctly answered question at Parse string to DateTime in C# however, I cannot get the answer to work.
Here is my code:
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd H:mm:ss.yyy";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);
Error:
DateTime pattern 'y' appears more than once with different values.
Note: error reported in original version of the post does not show up in this sample, but may be related:
"When converting a string to DateTime, parse the string before putting each variable into the DateTime object."
Your format should be yyyy-MM-dd HH:mm:ss.fff
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd HH:mm:ss.fff";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);
See: Custom Date and Time Format Strings
The error I get with that code is the following:
DateTime pattern 'y' appears more than once with different values.
It's pretty self-explanatory. Looking at the docs, you need to use .fff here:
"yyyy-MM-dd H:mm:ss.fff"
yyy is: The year, with a minimum of three digits, but since you already have yyyy in your pattern, you get the duplicate specifier error.
Your format is wrong, you used y twice.
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd H:mm:ss.fff";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);

Convert date yyyyMMdd to system.datetime format [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to convert date from yyyyMMdd format to mm-dd-yyyy fomrat
I have a string which contains date in yyyyMMdd format. I want to convert that date into system date format using ConvertTo.DateTime() method or any other simple method.
string value = "19851231"; //yyyyMMdd
DateTime dateTime = 1985/12/31;
string time = "19851231";
DateTime theTime= DateTime.ParseExact(time,
"yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
have at look at the static methods DateTime.Parse() and DateTime.TryParse(). They will allow you to pass in your date string and a format string, and get a DateTime object in return.
http://msdn.microsoft.com/en-us/library/6fw7727c.aspx

Categories