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.
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 have a string:
var stringDate = "8/19/2016 5:00:00 PM"
and I try using:
var dateTime = DateTime.ParseExact(stringDate,"M/d/yyyy h:mm:ss tt", null);
or
var dateTime = DateTime.ParseExact(stringDate, "M/d/yyyy h:mm:ss tt",
CultureInfo.Invariant);
or
var dateTime = DateTime.ParseExact(stringDate,"M/d/yyyy h:mm:ss tt",
CultureInfo.GetCultureInfo("en-us"));
It works fine if the machine is in the US but and for all 3 options I get the following error when running from a machine in London:
String was not recognized as a valid DateTime
what am I missing here?
As far as I can see, you need to use h specifier instead of hh specifier since your hour part does not have leading zero for single digit values.
var stringDate = "8/19/2016 5:00:00 PM";
var dateTime = DateTime.ParseExact(stringDate, "M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Also I would suggest to use specific culture settings instead of null for any case. In your example, since null means CurrentCulture settings for DateTime parsing methods, your CurrentCulture settings;
May not use / as a DateSeparator or
May not use : as a TimeSeparator or
May not use GregorianCalendar as a Calendar property or
May not have AM or PM as a PMDesignator or AMDesignator properties.
For those, your ParseExact method throws exception even if your string and format perfectly matches.
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.