How to find exact format of a date string? - c#

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");
}

Related

Not able to convert a string value to Date time including milliseconds

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);

String was not recognized as a valid datetime exception with DateTime.PraseExact

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.

convert string to datetime with form yyyy-MM-dd HH:mm:ss in C#

How can I convert this 2014-01-01 23:00:00 to DateTime I have done like this:
Console.WriteLine(DateTime.ParseExact("2014-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
and the result was like this :
1/1/2014 11:00:00 PM
this thing drive me crazy because this format was working in java.
I think your parsing worked. The problem is when converting back to string. You can provide the desired format in parameter :
DateTime date = DateTime.ParseExact("2010-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
string formattedDate = date.ToString("yyyy-MM-dd HH:mm:ss")
Console.WriteLine(formattedDate);
By default (without a specified format), it uses formatting information derived from the current culture.
Because 2014-01-01 23:00:00 IS 2014-01-01 11:00:00 PM.
Better explanation
You are implicitly calling DateTime.ToString(), which by default uses the General ("G") format, which in the en-US culture is MM/dd/yyyy hh:mm:ss tt.
If you want to display the time in a different format, you need to specify it:
string s = DateTime.ParseExact("2010-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(s.ToString("yyyy-MM-dd HH:mm:ss");
Or since you're using the same format string, just store it:
string format = "yyyy-MM-dd HH:mm:ss";
DateTime dt = DateTime.ParseExact("2010-01-01 23:00:00", format , CultureInfo.InvariantCulture);
Console.WriteLine(s.ToString(format));

C# DateTime Conversion from yyyy-MM-ddTHH:mm:ss to dd MMM yyyy

How to convert "yyyy-MM-ddTHH:mm:ss" into "dd MMM yyyy" format? For Instance, i want to convert 2013-04-16 05:30:05 into 16 April 2013. What is the correct method to achieve this?
First ParseExact then do ToString (I assume that you have string object, if you have DateTime object, skip first line)
var dateTime = DateTime.ParseExact(yourDateString, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
var yourNewString = dateTime.ToString("dd MMM yyyy");
Note that representation of DateTime you see in debugger is dependant on your current culture.
First, a DateTime has no format. But if you've already a string that represents a DateTime with the format yyyy-MM-ddTHH:mm:ss and you want to convert it to a string-date with format dd MMM yyyy you need to parse it to DateTime first.
Therefore use DateTime.ParseExact:
DateTime dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
Now you can use DateTime.ToString:
string result = dt.ToString("dd MMM yyyy");
Note that you need to pass a different CultureInfo object to ParseExact/ToString if you want to parse with another DateTimeFormat than your current (f.e. force english month names instead of german: dt.ToString("dd MMM yyyy", CultureInfo.InvariantCulture)).
As others mentioned, a DateTime has no format. To parse a string literal to a Date you need to call DateTime.Parse (if the string is in a culture-specific format) or DateTime.ParseExact if you need to pass a format string.
The format can be a custom format like yyyy-MM-dd HH:mm:ss or one of the standard format strings, eg. s for yyyy-MM-ddTHH:mm:ss.
2013-04-16 05:30:05 it not in one of the standard formats, so you have to parse by passing a custom format string:
var dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
On the other hand, yyyy-MM-ddTHH:mm:ss is the s standard format so you can just write:
var dt = DateTime.ParseExact("2013-04-16T05:30:05", "s", null);

String to 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.

Categories

Resources