Convert string to datetime fails - c#

I can't seem to find how to convert a string to a datetime. Here is what my date looks like: "23-nov-12".
I tried the following:
DateTime convertedDate = DateTime.ParseExact("dd-MMM-yy", "23-nov-12", CultureInfo.InvariantCulture);
and also
DateTime convertedDate = DateTime.ParseExact("0:d-MMM-yy", "23-nov-12", CultureInfo.InvariantCulture);
But I always get the following error:
String was not recognized as a valid DateTime.

Your arguments are in the wrong order. The date goes first.
DateTime convertedDate = DateTime.ParseExact("23-nov-12", "dd-MMM-yy", CultureInfo.InvariantCulture);
See: DateTime.ParseExact()

You got the parameters backwards.
The input string goes first.
DateTime.ParseExact("23-nov-12", "dd-MMM-yy", CultureInfo.InvariantCulture)

This code Works...
string dt = "23-nov-12";
Console.WriteLine(Convert.ToDateTime(dt));
It produces 11/23/2012 12:00:00 AM as output
Try It!

Related

Convert a string has datetime to DateTime

I want to convert a string of date and time to DateTime structure, but it is giving this error :
String was not recognized as a valid DateTime
DateTime dt = Convert.ToDateTime("5/15/2018 11:54:18 AM");
string date= dt.ToString("HH:mm");
I'm reading this question but I can't solve this code. What is my mistake?
What is the difference between Convert.ToDateTimeand DateTime.ParseExact() in C#?
Based on all the comments, here's how your code should look like
DateTime dt = DateTime.ParseExact("05/15/2018 11:54:18 AM", "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
string date = dt.ToString("HH:mm");
What is my mistake?
You Mistake is You are Providing argument Convert.ToDateTime() in a wrong Format.
try Providing "DD/MM/YYYY HH:MM:SS" according to Your system date Time Format .Else you need to use TryParseExatct with Specifying Format

How can I convert this date "03/01/2018 12:00 AM" to "2018-03-01" in C#?

I am using this code to convert a date 03/01/2018 12:00 AM to 2018-03-01 in C#:
DateTime startDate = DateTime.ParseExact(TextBox1.Text.ToString(),
"yyyy-mm-dd",
System.Globalization.CultureInfo.InvariantCulture);
but it throws an exception
String was not recognized as valid datetime
This Will Work Like A Charm
string bs = "03/01/2018 12:00 AM";
String startDate = DateTime.ParseExact(bs,"MM/dd/yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
Console.WriteLine(startDate);
// Outputs 2018-03-01
You can verify the code here
A DateTime represents a particular point in time. ParseExact is a way of turning a string into a DateTime. By saying ParseExact with "yyyy-mm-dd" you are telling it that the string you are giving it begins with a four digit year, which it doesn't. Fix the format string that you are supplying so that the parse works.
Once you have the value in your DateTime variable, you can use ToString("yyyy-mm-dd") to turn that DateTime back into a string.
Remove AM from your Textbox and edit format string, then your sample code will work.
This line run successfully :
DateTime startDate = DateTime.ParseExact("03/01/2018 12:00", "MM/dd/yyyy hh:mm", System.Globalization.CultureInfo.InvariantCulture);
string inputDate = "03/01/2018 12:00 AM";
string outputDate = DateTime.Parse(inputDate).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

getting issue with conversion from string to DateTime

I am getting issue while converting string to DateTime.
The value I am receiving as "08-26-2015 10:14:57.898Z".
I am trying to convert the above string to DateTime.
My Code:
DateTime.ParseExact(element.Value,"MM/dd/yyyy HH:mm:ss",CultureInfo.CurrentCulture);
Exception:
String was not recognized as a valid DateTime.
You have string with different format than you trying for conversion.
Try this
var input = "08-26-2015 10:14:57.898Z";
var date = DateTime.ParseExact(input, "MM-dd-yyyy hh:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
You can use:
DateTime dt = DateTime.ParseExact("08-26-2015 10:14:57.898Z", "MM-dd-yyyy hh:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
If you use CultureInfo.CurrentCulture(or null) the slash / has a special meaning. It is replaced with the current culture's date separator. Since that is not - but / in US you get an exception. Read
Have you tried Convert.ToDateTime ?
I just tried with your string and it works fine :
var s = "08-26-2015 10:14:57.898Z";
var date = Convert.ToDateTime(s);
String s = "08-26-2015 10:14:57.898Z";
DateTime date;
DateTime.TryParse (s, out date);
Now date variable contains DateTime value you need.

date conversion error

I am developing windows application.
In that i have date in the string format as>> fileDate="15/03/2013"
I want it to be get converted into date format as my database field is datetime.
I used following things for it>>
DateTime dt = DateTime.ParseExact(fileDate, "yyyyy-DD-MM", CultureInfo.InvariantCulture);
DateTime dt = DateTime.Parse(fileDate);
Both of these methods proved failure giving me error>>
String was not recognized as a valid DateTime.
What can be mistake?
Is there another technique to do that?
string fileDate = "15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
You have to give the date format according to the date string you have to ParseExact. You can see more on Custom DateTime format - MSDN
Change
"yyyy-MM-dd HH:ss"
To
"dd/MM/yyyy"
Your code would be
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You should do this:
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy",CultureInfo.InvariantCulture);
You must pass in the string for the format ("dd/MM/yyyy") in the same style that you pass in the string fileDate.
u may try with this
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse("ur_dateString")
In your current code you are using format "yyyyy-DD-MM" which is wrong since date part require lower case d not upper case D. , Also for year part you are specifying 5 ys, it should be 4, like yyyy, the order according to your date string should be: "dd/MM/yyyy". To be on the safe side you can even use "d/M/yyyy", which would work for single digit or double digit day/month.
So your code should be:
string fileDate="15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You can see more on Custom DateTime format - MSDN
It's because string "15/03/2013" cannot really be parsed as DateTime with format string "yyyy-MM-dd HH:ss".

How can I parse this date format into a DateTime object?

I have a date of the following format in a file (YYYY-MM-DD HH:MM:SS.millisecs):
1987-04-03 19:17:12.000
When I use DateTime to parse this string, it gets only the date part and does not get the time part. Can someone please tell me how to parse this into the DateTime object?
Use DateTime.ParseExact().
var format = "yyyy-MM-dd hh:mm:ss.fff"
var dt = DateTime.ParseExact(s, format);
See http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx. You should also add a format provider, just as CultureInfo.InvariantCulture.
var dt = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture);
I did up a quick console app and it's showing both date and time:
string dateTimeString = "1987-04-03 19:17:12.000";
Console.WriteLine(DateTime.Parse(dateTimeString));
Console.ReadLine();
The resulting output is:
4/3/1987 7:17:12 PM
You might be using the resulting parse value incorrectly?
DateTime.Parse("1987-04-03 19:17:12.000") returns 4/3/1987 7:17:12 PM
The format string you are looking for is:
yyyy-MM-dd HH:mm:ss.fff
So
DateTime myDateTime = DateTime.ParseExact(sourceString, "yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvarientCulture);
See MSDN for more information
How are you parsing it? How are you converting the DateTime to a string?
DateTime date = DateTime.Parse ("1987-04-03 19:17:12.000");
Console.WriteLine (date);
// yields: 4/3/1987 7:17:12 PM
Console.WriteLine (date.Date);
// yields: 4/3/1987
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime.ParseExact(timeString, "dd/MM/yyyy HH:mm:ss.fff",culture);

Categories