C# - String was not recognized as a valid DateTime [duplicate] - c#

This question already has answers here:
c# parse date time with timezone
(3 answers)
Closed 3 years ago.
I'm getting the error "string not recognized as a valid DateTime". I've tried numerous formats but continue to get the error. I want to return "10/31/2019".
string x = DateTime.ParseExact(
"2019-10-31T09:40:28-04:00",
"yyyy-MM-ddTHH:mm:ss-K",
DateTimeFormatInfo.InvariantInfo).ToString("d");

The format string should be yyyy-MM-ddTHH:mm:ssK or yyyy-MM-ddTHH:mm:sszzz. notice the lack of - in the format string.

Not sure what the -K is doing, you need a zzz there.
"yyyy-MM-ddTHH:mm:sszzz" should be your formatter.

In your example the format for timezone is not -K. The minus character is part of the timezone (K format), not a separator between time and timezone. For example -04:00 means "minus four hours". So it should be:
string x = DateTime.ParseExact(
"2019-10-31T09:40:28-04:00",
"yyyy-MM-ddTHH:mm:ssK", // <-----
DateTimeFormatInfo.InvariantInfo).ToString("d");

The format string needs to modified as follows
string x = DateTime.ParseExact("2019-10-31T09:40:28-04:00", "yyyy-MM-ddTHH:mm:sszzz", DateTimeFormatInfo.InvariantInfo).ToString("d");

This worked.
string x = DateTime.ParseExact("2019-10-31T09:40:28-04:00", "yyyy-MM-ddTHH:mm:sszzz", DateTimeFormatInfo.InvariantInfo).ToString("d");

Related

How to convert date "2018-12-13T07:33:35.893Z" to 13/12/2018 [duplicate]

This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Convert datetime without timezone
(4 answers)
Closed 4 years ago.
I have tried following way but doesn't work
dateTime="2018-12-13T07:33:35.893Z"
DateTime dt;
DateTime.TryParseExact(dateTime, out dt);
But I am always getting dt as {1/1/0001 12:00:00 AM}.
Can you please tell me why? and how can I convert that string to date?
I also tried Convert.ToDateTime but doesn't work.
What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.
Have you got the original DateTime object or you simply have it in a string?
In case you've got it as DateTime:
string european = dateTime.ToString("dd/MM/yyyy");
In case you've got it as a string:
string date = "2018-12-13T07:33:35.893Z";
if(DateTime.TryParse(date , out DateTime result))
result.ToString("dd/MM/yyyy");
Have a look at the original MSDN documentation about the DateTime.ToString method
Since you've got a DateTime you can convert to that format:
var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");
With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(#"MM\/dd\/yyyy");

.Net DateTime.ParseExact not working [duplicate]

This question already has answers here:
How to convert datetime string in format MMMdyyyyhhmmtt to datetime object?
(2 answers)
Closed 6 years ago.
should the following parsing works,
DateTime.ParseExact("20150105 91353", "yyyyMMdd Hmmss", CultureInfo.InvariantCulture);;
I found out the above doesnt work, while the below, works,
DateTime.ParseExact("20150105 091353", "yyyyMMdd HHmmss", CultureInfo.InvariantCulture);;
I would like to know what is wrong with the first line of code.
This is one of the special cases where Custom DateTime format might find the input ambiguous.
When you do not have separator between hour and minutes, the single H format cannot distinguish the second number belongs to the hour or the minutes, thus your parse failed.
91353 //interpreted either as 9 13 53 or 91 35 3 - which one? ambiguous -> error
But this is ok:
string str = "20150105 9:13:53"; //no ambiguity with format yyyyMMdd H:mm:ss
string fmt = "yyyyMMdd H:mm:ss"; //can handle both "20150105 9:13:53" and "20150105 09:13:53"
DateTime dt = DateTime.ParseExact(str, fmt, CultureInfo.InvariantCulture);
To solve it, try to do little manipulation on your original string.
string dtstr = "20150105 91353";
string fmt = "yyyyMMdd Hmmss";
string[] parts = dtstr.Split(' ');
string str = parts[1].Length < 6 ? string.Join(" 0", parts) : dtstr;
DateTime dt = DateTime.ParseExact(str, fmt, CultureInfo.InvariantCulture);
Then it should be OK.
According to MSDN
If you do not use date or time separators in a custom format pattern, use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the pattern, specify the wider form, "HH", instead of the narrower form, "H".
This means this is correct DateTime.ParseExact("20150105 9:13:53", "yyyyMMdd H:mm:ss", CultureInfo.InvariantCulture); because it's using time separators

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);

Convert month string to int format [duplicate]

This question already has answers here:
How to parse a month name (string) to an integer for comparison in C#?
(15 answers)
Closed 8 years ago.
I have the following string "Dec".
How can I convert this string to the month number 12?
I tried this:
DateTime.ParseExact("Dec", "MMMM", null).Month
This doesn't work.
You can also use ParseExact method. i.e,
string month = "Dec";
int MonthDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;
Simply use "MMM":
DateTime.ParseExact("Dec", "MMM", null).Month
also best use a specific culture, so its consisten with the input:
DateTime.ParseExact("Dec", "MMM", System.Globalization.CultureInfo("en-us")).Month
The long version MMMM refers to a full month name as originaly posted here How to parse a month name (string) to an integer for comparison in C#?

Convert string to decimal fail [duplicate]

This question already has answers here:
Decimal.Parse and incorrect string format error
(3 answers)
Closed 8 years ago.
because I can not convert correctly? what am I doing wrong? I Need My variable value is identical to the string type .
I have to use another type ?
static void Stringparadecimal()
{
string vcarga = "1324.54";
decimal vcargadec = decimal.Parse(vcarga);
Console.WriteLine("Convertendo String vcargad para formato decimal ");
Console.WriteLine("Tipo da variavel vcargadec : " + vcargadec.GetType());
}
Specify the format info by supplying the correct culture (in this case, InvariantCulture means US English):
decimal.Parse("1324.54", CultureInfo.InvariantCulture)
You have to take into account the culture in which you are parsing the decimal value. Some cultures consider '.' to be the decimal point, some other cultures consider ',' instead. It is always a good practice to specify the culture to use when doing decimal values parsing. In your case, supply CultureInfo.InvariantCulture to the Double.Parse method.
try this one...
decimal d = decimal.Parse("1324.54", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
Convert.ToDecimal("1324.54", System.Globalization.CultureInfo("en-US"), "{0:c}".
You may have to change from en-US to whichever country that you're writing to code for.

Categories