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/
Related
I'm trying to convert a string into a DateTime in C#, but I'm getting this error:
System.FormatException: 'String was not recognized as a valid DateTime.'
The error is in the next line:
DateTime endTime = DateTime.ParseExact(endDate, "MM/dd/yyyy hh:mm:ss tt", null);
My endDate variable has the following info: "10/03/2017 06:52:48 AM"
What am I doing wrong?
When you use null as an IFormatProvider, all DateTime parsing methods use CurrentCulture settings of your computer.
There are a few possibilities that you get exception. For example, your CurrentCulture might not have AM as it's AMDesignator property.
Instead of that, use a proper culture like InvariantCulture.
DateTime endTime = DateTime.ParseExact("10/03/2017 06:52:48 AM",
"MM/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);
I have the following output in string:
24/05/15 11:40:50 AM
now I want to convert this string into -> 2015-05-24 11:40:50.000
I have tried below method but it gives me error :
String was not recognized as a valid DateTime.
DateTime.ParseExact("24/05/15 11:40:50 AM",
"yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture);
From 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.
In your case, they are not.
First, you can parse it to DateTime with specific format and you can generate a string representation with a specific format from that DateTime. Like;
string s = "24/05/15 11:40:50 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yy hh:mm:ss tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss.fff"));
}
Prints;
2015-05-24 11:40:50.000
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 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.