This question already has answers here:
.net: How to parse a date with this format: 08 Feb 2011 06:46
(3 answers)
Closed 4 years ago.
That question may be already been answered here, but I could not find any answers.
I'm trying to to convert the string 25APR18 to 2018-04-25.
I tried to do the following:
DateTime.ParseExact("25APR18", "yyyy-mm-dd", CultureInfo.InvariantCulture);
But it does not convert and gives me the error saying "String was not recognized as a valid DateTime."
I was not able to find the right solution.
Any suggestions?
You can use:
DateTime date = DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);
The string format is case sensitive representing the input format you want to match. Lower case mm represents minutes but you need month abbreviated name MMM and that's one issue with the code you posted. Full list of format specifiers can be found here.
Once you have a DateTime object you can retrieve the date as string with new format:
string formattedDate = date.ToString("yyyy-MM-dd");
Which produces:
2018-04-25
Your date parse code should be
DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);
Related
This question already has answers here:
unable to parse a string of this format "1/29/2020 12:00:00 AM" into a valid DateTime
(2 answers)
Closed 2 years ago.
I have a date time column named ProjectLastUpdate which has values such as 2/22/2020 11:29:52 PM & 1/29/2020 12:00:00 AM..
I wrote the following code:
DateTime.ParseExact(projectLastUpdate.ToString(), "M'/'d'/'yyyy' 'H':'m':'s", CultureInfo.InvariantCulture).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'")
to convert the date to the ISO standard, but I am getting this exception:
String was not recognized as a valid DateTime
The format parameter of ParseExact function is not correctly specified. You can change the statement as:
DateTime.ParseExact(projectLastUpdate.ToString(), "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture)
.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");
Based on your code-snippet it seems projectLastUpdate is in string format. If so, you could have avoided ToString() conversion.
This question already has answers here:
Converting a String to DateTime
(17 answers)
Parse a string containing date and time in a custom format
(3 answers)
Closed 4 years ago.
I have a string: "20180830" which represents 30 august 2018
I want to go to string: "30/08/2018"
So that I can do: DateTime parsedDate = DateTime.Parse("30/08/2018"); and have a DateTime instead of a string,
Tried everything but didn't succeed.
Needs some help.
You can use the DateTime.ParseExact-Method to solve your problem. Therefore you need to specify the exact format which would be yyyyMMdd in your case. Also the documentation suggests to use CultureInfo.InvariantCulture.
The following code...
DateTime datetime = DateTime.ParseExact("20180830", "yyyyMMdd", CultureInfo.InvariantCulture);
...should do the trick ;-)
This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
Closed 5 years ago.
I'm trying to translate a Date in the current system language, this is my date: 2018/01/01, I tried to achieve my goal in this way:
var date = DateTime.ParseExact("2018/01/01", "dddd MMMM yyyy", new CultureInfo("it-IT")).ToString();
unfortunately I get:
System.ArgumentNullException
The InnerException say:
String not recognized as a valid DateTime value.
I used ParseExact to avoid this error, what I did wrong?
Use this code it will work.
var date = DateTime.ParseExact("2018/01/01", "yyyy/MM/dd", new CultureInfo("it-IT"));
I think you now know the error reason.
Thanks
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 7 years ago.
I am trying to read date from excel using code
String ss = (String)w.Cells[2 + i, 4].Value2;
dRow[3] = DateTime.Parse(ss);
Code works when ss = "12/11/2015" but gives error
String was not recognized as a valid DateTime
when ss = "13/11/2015"
It gives error because month can not be 12 but it is taking date as month. This is what I think. Same code is working on other PC. Do I need to check my date time format or anything like date setting.
DateTime.Parse uses standard date and time format of your CurrentCulture settings by default.
Looks like your CurrentCulture has MM/dd/yyyy format as a short date format and since there is no month as 13 in Gregorian Calendar (which probably uses as a Calendar for your CurrentCulture), you get FormatExcetion.
You can use DateTime.ParseExact method to specify your format exactly like;
dRow[3] = DateTime.ParseExact("13/11/2015", "dd/MM/yyyy",
CultureInfo.InvariantCulture);
If you get this as an input and you want to parse it to DateTime, you have to know which format it has. Other than that, it can generate ambiguous scenarios.
For example; what 01/02/2015 should be parsed as? 1st February or 2nd January?
You shouldn't assume that every string you supplied perfectly parsed with DateTime.Parse method. It is not that smart.
This question already has answers here:
format date in c#
(7 answers)
Closed 8 years ago.
i have this string "1/20/2015 12:00:00 AM" and i need to convert in something like this "20/1/2015" without time and days first.
DateTime mydate = Convert.ToDateTime(myDateFromDB);
To convert date string of format "dd/MM/yyyy" to DateTime, should used DateTime.TryParseExact where you can specify the date format string to let the converter know about the Culture.
To get the Date alone, use myDateTimeObj.Date.