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");
Related
This question already has answers here:
Parse datetime in multiple formats
(7 answers)
Closed 5 years ago.
I have multiple input in string like the following:-
05/09/2017
05/09/2017 13:56 PM
05/09/2017 01:56 PM
05/09/2017 13:56:00
05/09/2017 01:56:00 PM
Now how do i convert the above examples into DateTime format (dd/MM/yyyy hh:mm:ss tt).
I have already tried
ParseExact - It gives error when the user gives 05/09/2017 as value (because the format doesn't match)
DateTime.ParseExact(ValueByuser, "dd/MM/yyyy HH:mm:ss tt", null);
TryParse - The problem with it is that it uses the LocalFormat of the computer like if the computer has set to "MM/dd/yyyy" then it produces output in the same format
DateTime.TryParse(DatePass, out Dtp);
You could use the ParseExact or TryParseExact overload that accepts an array of formats, and pass all the formats you want to support. E.g.
var formats = new[]
{
"dd/MM/yyyy",
"dd/MM/yyyy HH:mm",
"dd/MM/yyyy HH:mm tt",
"dd/MM/yyyy HH:mm:ss",
"dd/MM/yyyy HH:mm:ss tt",
};
You should also specify CultureInfo.InvariantCulture as your format provider, e.g.:
var d = DateTime.ParseExact(s,
formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
I believe the above will match all your sample inputs except the second (05/09/2017 13:56 PM) which I assume is a typo and should be 05/09/2017 13:56 without the am/pm indicator.
You could do something like the following;
string dd = "05 / 09 / 2017 13:56:00";
string newdate = dd.Replace("/", "-");
DateTime DT = DateTime.Parse(newdate);
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"));
I need to compare two date format strings:
dateString in "dd-MMM-yy" format
with
referenceDateString in "M/dd/yyyy hh:mm:ss tt" format respectively.
For that, I need to convert the dateString = "dd-MMM-yy" to "M/dd/yyyy hh:mm:ss tt".
However, Got an error while trying to do that:
"Error: string was not recognized as a valid datetime".
The C# code I used given below.
string dateString = "19-Dec-14";
string AsofDate = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Edit 1:
In the actual code the dateString obtaining after reading a csv file which is supplied as "19-Dec-14", that's why it's in the string format.
Please help, am pretty new to C#. Thanks.
Habib already gave the answer on his comments, I try to add it as an answer;
From DateTime.ParseExact(String, String, IFormatProvider)
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
In your case, clearly they don't. First, you need to parse your string to DateTime with proper format (which is dd-MMM-yy with an english-based culture), then you can get the string represention of your DateTime with specific format.
string s = "19-Dec-14";
DateTime dt;
if(DateTime.TryParseExact(s, "dd-MMM-yy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// Result will be 12/19/2014 12:00:00 AM
}
It's not entirely clear what you are trying to do, but in order to parse that date you have on the first line, you would use something like this:
DateTime AsofDate = DateTime.ParseExact(dateString, "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
Note a couple things here: I've changed the data type of AsofDate from string to DateTime because that's what DateTime.ParseExact returns. Also, I've modified the custom format string to match the format of the string you are trying to parse as a date ("19-Dec-14").
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);
I am trying to convert the string value in the date time. I know this question was asked so many times. But I checked mostly all the answers. But I didn't get answer for my problem.
Following is my code:
string ObjTime = "5/4/2013 10:30 PM";
DateTime d = DateTime.ParseExact(ObjTime, "dd/MM/yyyy H:mm", CultureInfo.CurrentCulture);
I have also checked by chenging my System datetime format.
And also I have use this:
DateTime d = DateTime.ParseExact(ObjTime, "d/M/yyyy H:mm tt", CultureInfo.CurrentCulture);
Can any one please help me to solve this problem?
And also I have check by changing the format as d/m/yyy H:mm but still it is giving me error. I am using Visual Studio 2012.
I checked the problem and it seems like your string is not in correct format hence the ParseExact is throwing error.
If you change your string from
string ObjTime = "5/4/2013 10:30 PM";
to
string ObjTime = "05/04/2013 10:30 PM";
The code works
Also checked this
Your string has day and month in single digit, and you are trying to parse it with format which supports only double digits day/month
You should do:
string ObjTime = "5/4/2013 10:30 PM";
DateTime d = DateTime.ParseExact(ObjTime, "d/M/yyyy h:mm tt", CultureInfo.CurrentCulture);
You should use single d and M, which would support single digit and double digit day/month for parsing.
You should also use lower case h since you have PM in the string. So your final format should be "d/M/yyyy h:mm tt"
Instead of specifying the format string explicitly, you can try specifying the culture, in which this format is valid. For EN-US culture:
DateTime.Parse(ObjTime, CultureInfo.GetCultureInfo("en-us"));