I am trying to convert 10/10/2010 12:00:00 a.m. to dateTime . I am using this...
DateTime.ParseExact(item.Birthey, "dd/MM/yyyy hh:mm:ss tt", null);
But I get String was not recognized as a valid DateTime.
What is the correct way to do this?
Just simply try the Parse or ParseExact with manipulation of string command like:
string stringDate= "10/10/2010 12:00:00 P.M.";
var myDate = DateTime.Parse(stringDate.Replace(".", ""));
You may also try the following code sample if, the raw data that you get has "a.m." ending in it. Just replace that characters:
string stringDate= "10/10/2010 12:00:00 a.m.";
DateTime result = DateTime.ParseExact(stringDate.Replace(".", ""),
"dd/MM/yyyy hh:mm:ss tt", null);
Try instead
DateTime.ParseExact("10/10/2010 12:00:00 am", "dd/MM/yyyy hh:mm:ss tt", null).Dump();
the . in a.m. are creating the problems. You can use Replace to remove the . (as in your date format . can only appear in a.m., or p.m.
DateTime.ParseExact("10/10/2010 12:00:00 a.m.".Replace(".",""), "dd/MM/yyyy hh:mm:ss tt", null).Dump();
Try to specify CultureInfo.InvariantCulture instead of null. In your case it uses CultureInfo.CurrentCulture.
Related
I have a string value which I need to convert to DateTime
currentDate="29-APR-17 03.42.06.410000 PM"
I tried the below statement, but its throwing error:
DateTime originalDate = DateTime.Parse(currentDate);
DateTime originalDate = Convert.ToDateTime(currentDate);
I even followed this link "https://msdn.microsoft.com/en-us/library/bb882581(v=vs.110).aspx"
but is is also not helping
Moreover, the information provided in Stackoverflow was not helping as I am always facing the error String was not recognized as a valid DateTime.
The DateTime.ParseExact should solve the formatting like this:
DateTime originalDate = DateTime.ParseExact(currentDate, "dd-MMM-yy hh.mm.ss.ffffff tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
or if time part are separated with colons:
DateTime.ParseExact(currentDateWithColons, "dd-MMM-yy hh:mm:ss.ffffff tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
Both dots and colons producing the same result using Console.WriteLine(originalDate.ToString("dd/MM/yyyy hh:mm:ss.ffffff tt")):
29/04/2017 03:42:06.410000 PM
Working example: .NET Fiddle Demo
try below code
string currentDate="29-APR-17 03:42:06.410000 PM"
DateTime originalDate = DateTime.Parse(currentDate);
originalDate = Convert.ToDateTime(currentDate);
Console.WriteLine(originalDate.ToString("dd-MM-yyyy-fff"));
you were using wrong format use : instead of . in time.
changed "29-APR-17 03.42.06.410000 PM" to "29-APR-17 03:42:06.410000 PM"
string currentDate = "29-APR-17 03:42:06.410000 PM";
var date = DateTime.ParseExact(currentDate, "dd-MMM-yy hh:mm:ss.ffffff tt", CultureInfo.InvariantCulture);
I'm using the following code in linqpad (remove the .Dump() for native C#):
string dateTime = "3/20/2015 1:45:00 PM";
string dateFormat = "M/d/yyyy hh:mm:ss tt";
DateTime timeResult;
bool parsed = DateTime.TryParseExact(dateTime, dateFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out timeResult);
parsed.Dump();
timeResult.Dump();
However, the parse is false and the date is 0001-01-01 12:00:00 AM.
I can't see any issue in my format string. I tried updating it to "MM/dd/yyyy hh:mm:ss tt" with no changing effect.
Could anyone tell me where I'm going wrong?
Change the string to ""M/d/yyyy h:mm:ss tt";" with a single h. For that matter, you might as well have to change to h:m:s if your minutes and seconds are in that format too.
I want to convert date time in to a particular format and save it in to a variable of type Object , but I am facing the error as String was not recognized as a valid DateTime
below the code which I tried
string regDate = DateTime.ParseExact("05/07/2015 19:41:06 PM", "MM-dd-yyyy HH:mm tt", CultureInfo.InvariantCulture);
The input will be in the format of "05/07/2015 19:41:06 PM" and i want the output in the mm/dd/yyyy format with hours-mins-secs also.
The error you get is reasonable, because the string you pass hasn't the exact format, you pass to the ParseExact.
Please try the following:
var regDate = DateTime.ParseExact("05/07/2015 19:41:06 PM", "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
Furthermore, there is no need you convert a string to a string, like you did here Convert.ToString("05/07/2015 19:41:06 PM").
Check this .NET Fiddle.
I can't comment or I would. You need to combine Christos' answer and Imranullah Khan's comment
var regDate = DateTime.ParseExact("05/07/2015 19:41:06", "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
or
var regDate = DateTime.ParseExact("05/07/2015 07:41:06 PM", "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
You can't include AM/PM with 24 hour times (19:41:06). So either drop the PM and tt or include the tt and change the HH to hh and then change the time to 07:41:06.
I need to know exact date format that will perse the string 16-Aug-78 12:00:00 AM. I have tried various string like "ss-MMM-yy hh:mm:ss". Is there any way for finding it to converting to any general format. I am using CultureInfo.InvariantCulture class.
Your string format is wrong. It has to match your string format exactly. You can use dd-MMM-yy hh:mm:ss tt format instead.
Here an example in LINQPad.
string s = "16-Aug-78 12:00:00 AM";
var date = DateTime.ParseExact(s, "dd-MMM-yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
date.Dump();
Custom Date and Time Format Strings
Or, since dd-MMM-yy hh:mm:ss tt is a standart date and time format for InvariantCulture, you can directly DateTime.Parse method like;
string s = "16-Aug-78 12:00:00 AM";
var date = DateTime.Parse(s, CultureInfo.InvariantCulture);
date.Dump();
Here a demonstration.
Is there any way for finding it to converting to any general format?
There is no way to get format of a string except you create your own formatting. Only you can know what is your string format exactly, computer can't.
For example; 01/02/2014 can be 1 February 2014 or 2 January 2014 depends on which custom format you can parse it.
Try as below
var dstring = "16-Aug-78 12:00:00 AM";
DateTime result;
var matchingCulture = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(ci => DateTime.TryParse(dstring, ci, DateTimeStyles.None, out result))
You have wrong format string, you are using ss for day it should be dd. This article Custom Date and Time Format Strings explains what you need for custom format.
Use
"dd-MMM-yy hh:mm:ss tt"
Instead of
"ss-MMM-yy hh:mm:ss"
You can use DateTime.ParseExact to parse the string.
DateTime dt = DateTime.ParseExact("16-Aug-78 12:00:00 AM", "dd-MMM-yy hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
You can try with DateTime.TryParseExact():
string strDate = "16-Aug-78 12:00:00 AM";
DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] {"dd-MMM-yy hh:mm:ss tt" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datDate))
{
Console.WriteLine(datDate);
}
else
{
Console.WriteLine("Error in datetime format");
}
I am getting dates from database and i want to convert the date in dd/MM/yyyy format,
I am trying this but it gives me error "String was not recognized as a valid DateTime."
DateTime pDate = DateTime.ParseExact("05/28/2013 12:00:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Please let me know how i can convert into dd/MM/yyyy format?
Thanks
try with MM/dd/yyyy hh:mm:ss tt instead of dd/MM/yyyy
if you use DateTime.ParseExact The format of the string representation must match a specified format exactly or an exception is thrown.
if you need only the date
DateTime pDate = DateTime.ParseExact("05/28/2013 12:00:00 AM", "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
var dateOnly= pDate.ToString("dd/MM/yyyy");
As per your comment, database contain data with Type 'Date', So you can read it directly as DateTime. You can convert to string by calling ToString with the expected Format.
2 steps:
DateTime pDate = DateTime.ParseExact("05/28/2013 12:00:00 AM", "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
return pDate.ToString("dd/MM/yyyy");
DateTime d = DateTime.Parse(
"15/12/2019",
new System.Globalization.CultureInfo("pt-BR"));
refer https://blog.submain.com/string-was-not-recognized-as-valid-datetime/