Format String to specific datetime [duplicate] - c#

This question already has answers here:
CONVERT MM/DD/YYYY HH:MI:SS AM/PM to DD/MM/YYYY in C#
(3 answers)
Closed 8 years ago.
I want to convert the string "2015/07/05" to the format 08-MAR-2015.
The code below keeps getting detected as an invalid datetime format (i.e the else statement below)
C# Code
string format = "dd-MMM-yyyy";
string dateString = "2015/07/05";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,DateTimeStyles.None, out dateTime))
{
MessageBox.Show(Convert.ToString(dateTime));
}
else // Invalid datetime format
{
MessageBox.Show("UBD date is not a valid date format: " + dateTime.ToString());
}

String formatting is performed by the String.Format method, not Convert.ToString. The Convert methods try to convert one type to another using the current culture's default format where required.
Try the following
String.Format(CultureInfo.InvariantCulture,"dd-MMM-yyyy",someDate);
This will ensure that the English month names will be used.
In non-English cultures the following line will return the local month name
String.Format("dd-MMM-yyyy",someDate);

Related

Converting string that is dd.mm.yyyy. HH:mm:ss format to datetime C# [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 3 years ago.
So I have string that is 24.08.2010. 21:21:21, I want to convert it to 2010-08-24 21:21:21 to be able to save it in the db.
I tried this
var input = "22.08.2010. 7:00:00";
var date = DateTime.ParseExact(input,"yy-MM-dd HH:mm:ss.fff", null);
Console.WriteLine(date);
but I get error:
System.FormatException: String was not recognized as a valid DateTime.
Anyone have idea how to convert this?
First, you need to convert string input into date:
var input = "22.08.2010. 7:00:00";
var date = DateTime.ParseExact(input, "dd.MM.yyyy. H:mm:ss", null);
And than convert date to string:
Console.WriteLine(date.ToString("yy-MM-dd HH:mm:ss"));

String was not recognized as a valid datetime - Datetime conversion issue [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 5 years ago.
I am having a date like "19/01/2018 15:30" as string.
I need to convert this to a DateTime object.
I am getting error:
String was not recognized as a valid datetime
This is my code:
DateTime drawDatetime = DateTime.ParseExact("19/01/2018 15:30"
, "dd/MM/yyyy hh:mm:ss", null);
You format string contains seconds but the date doesn't. Try this instead:
DateTime drawDatetime = DateTime.ParseExact(
"19/01/2018 15:30",
"dd/MM/yyyy HH:mm", null);

Why does DateTime.ParseExact throw an exception with "dd.Mm.yyy" format and a date with a 4-digit year? [duplicate]

This question already has answers here:
Why can't DateTime.ParseExact parse DateTime output?
(4 answers)
Closed 5 years ago.
The documentation on yyy format says that it's 3 digits or more. So why does the following code throw an exception?
string format = "dd.MM.yyy";
string s = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
DateTime d = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture); //this throws FormatException
Fiddle
This looks like a bug to me. I tried this to rule out any locale problems and also got the same error:
string format = "dd.MM.yyy";
string s = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
DateTime d = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture); //this throws FormatException
Edit: Looks to be a duplicate of this question: Why can't DateTime.ParseExact parse DateTime output?
I tried debugging your code
The string stores all 4 digits but your format asks for 3 digits in year. I guess that is why DateTime is not able to parse you string
Try using yyyy when parsing it will solve your issue

String format with c# [duplicate]

This question already has answers here:
Converting dd/mm/yyyy formatted string to Datetime [duplicate]
(3 answers)
Closed 7 years ago.
I have this string value
var d = "2016-01-07 09"
It's a string that contains a date with hour without minutes, seconds and millis.
I want to trasform into Datetime with this format
2016-01-07 09:00:00:000
I know that it sounds stupid but this stuff makes me crazy.
Thanks for support.
You have a Date with format Year - Month - Day Hour (yyyy-MM-dd HH).
You can use DateTime ParseExact method . If you know your dates format. You can parse it .
Check dateformats on this link
string d= "2016-01-08 03";
var c = DateTime.ParseExact(d, "yyyy-MM-dd HH", CultureInfo.InvariantCulture);
return c;

C# casting String to DateTime [duplicate]

This question already has an answer here:
Parsing ISO 8601 with timezone to .NET datetime
(1 answer)
Closed 9 years ago.
I want to cast in C# a String to DateTime.
My String contains: String input = "2012-07-31T00:00:00.000+0200"
and i used the following pattern: String datePattern = "yyyy-MM-dd%HH:mm:ss.fffz";
MyDateTime myDate = new DateTime();
MyDateTime myDate = DateTime.ParseExact(input, datePattern, null);
And i am getting the following error: String was not recognized as a valid DateTime.
Was pretty sure, because i am not sure how to solve this 'T'and which Timezone i should use.
There are three variants of it at the msdn side.
Which one i need to use, or can i create my own one?
Any suggestions?
You can use The "K" Custom Format Specifier with "yyyy-MM-ddTHH:mm:ss.FFFK" format like;
string s = "2012-07-31T00:00:00.000+0200";
var date = DateTime.ParseExact(s, "yyyy-MM-dd'T'HH:mm:ss.FFFK", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be;
7/30/2012 10:00:00 PM
Here a demonstration.
For more information take a look at;
Custom Date and Time Format Strings

Categories

Resources