I have following strings in different formats:
16/05/2014
21-Jun-2014
2014-05-16
16-05-2014
5/19/2014
14 May 2014
I need to convert all the above strings into mm/dd/yyyy format in c#.
I have tried used DateTime.ParseExact as DateTime dt = DateTime.ParseExact("16-05-2014", "mm/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture) in C# but i am getting the exception as "String was not recognized as a valid DateTime".
I have also tried to use to Convert.ToDateTime() but it is also not working.
Is there any method or function that we can write/available in C# that would convert the above string formats into a single date format i.e into "mm/dd/yyyy" format ??
Any help on this would be greatly appreciated.
It fails on the very first term of your format string, which is telling the function to treat the "16" as minutes and to look for hours, minutes, and seconds that don't exist in the input.
You have several different date formats, and so need the ParseExact() overload that accepts several different format strings:
string[] formats= {"dd/MM/yyyy", "dd-MMM-yyyy", "yyyy-MM-dd",
"dd-MM-yyyy", "M/d/yyyy", "dd MMM yyyy"};
string converted = DateTime.ParseExact("16-05-2014", formats, CultureInfo.InvariantCulture, DateTimeStyles.None).ToString("MM/dd/yyyy");
Also remember that lower case "m"s are for minutes. If you want months, you need an upper case "M". Full documentation on format strings is here:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Finally, I suspect you are getting ahead of yourself on formatting the output as a string. Keep these values as DateTime objects for as long as possible, and only format to a string at the last possible moment before showing them to the user. If you really do want a string, at least stick with the ISO 8601 standard format.
Your main problem is that your format string is wrong. A small m is for minute, a big M is for month.
Try to pass all your formats in an array. For example like this
DateTime.ParseExact("16-05-2014",
new[] {"dd/MM/yyyy", "dd-MMM-yyyy", "yyyy-MM-dd",
"dd-MM-yyyy", "M/d/yyyy", "dd MMM yyyy"},
CultureInfo.InvariantCulture, DateTimeStyles.None);
With this you can parse all your formats at once.
For more information about the format settings, see the official docs.
Few things:
Your input date 16/05/2014 doesn't match your format Month/Day/Year - how can there be a 16th month?
Secondly, you're using mm which represents Minutes, not Months. You should use MM.
Finally, your sample string 16-05-2014 doesn't match the format provided, you've used hyphens - instead of forward slashes /
Supply a collection of different formats matching your input:
string[] formats = new [] { "MM/dd/yyyy", "dd-MMM-yyyy",
"yyyy-MM-dd", "dd-MM-yyyy", "dd MMM yyyy" };
DateTime dt = DateTime.ParseExact("05-16-2014", formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
You might find the following method useful to accept whatever date format you want and convert it to DateTime:
public DateTime? DTNullable(string DateTimestring, string CurrDateTimeFormat)
{
if (string.IsNullOrEmpty(DateTimestring)) return null;
else
{
DateTime datetimeNotNull;
DateTime.TryParseExact(DateTimestring, CurrDateTimeFormat, null, System.Globalization.DateTimeStyles.None, out datetimeNotNull);
return datetimeNotNull;
}
}
Pass in your desired string to be converted to DateTime along with it's current date time format and this would return you a nullable DateTime. If you're certain that whatever string you're passing in won't be null then you can remove that bit. The reason for it being there is that you can't convert a null to DateTime. In my case I couldn't be certain if it would be or not so I needed the ability to capture nulls as well.
You can use it like this:
DateTime? MyDateTime = DTNullable(MyStartDate, "dd/MM/yyyy");
If you wanted you could alter the method to accept an array of strings and simply iterate through each and return them all in a list if they were of the same format.
As others have pointed out, months are MM not mm (minutes).
On a DateTime object you can call .ToString("MM/dd/yyyy"). Given the strings you have, you can first create new DateTime objects for each string and then call .ToString("MM/dd/yyyy"). For example:
var dateAsMmDdYyyy = DateTime.Now.ToString("MM/dd/yyyy");
Related
How would you handle the following string value that needs to be converted to a DateTime object?
"2015/01/22 12:08:51 (GMT+09:00)"
Would like to include this as a recognized DateTime pattern. As I encounter other formats, I would like to just implement a new pattern.
Here a piece of code that will successfully parse the given string (notice DateTimeOffset rather than DateTime):
var str = "2015/01/22 12:08:51 (GMT+09:00)";
var dt = DateTimeOffset.ParseExact
(str,
"yyyy/MM/dd HH:mm:ss (\\G\\M\\TK)",
System.Globalization.CultureInfo.InvariantCulture
);
//dt now has +9:00 offset - that's correct only if GMT is provided as UTC.
More info at The Difference Between GMT and UTC
This code takes a string and converts it into a DateTime object
DateTime myDate = DateTime.Parse("2017-08-28 14:20:52,001", "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
All you need to do is create a format that matches your input. This link helps:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
For more details read this: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-convert-a-string-to-a-datetime
Here there is the official documentation of DateTime.Parse with some examples. It covers also the case of other formats
https://msdn.microsoft.com/it-it/library/system.datetime.parse(v=vs.110).aspx
Using DateTime.ParseExact is probably your best bet. It takes in an input string and an expected format string that the input should match. It will return true if the conversion was successful, and the out parameter will be the result of the conversion (result in the example below).
I was unable to get it to work without forcibly removing the "GMT" portion, but if that's acceptable to you, the code below should work.
This example takes the original input and converts it to UTC time (i.e. it adjusts the time based on your GMT value, which is to subtract 9 hours in your example):
var input = "2015/01/22 12:08:51 (GMT-08:00)";
var format = "yyyy/MM/dd H:mm:ss (zzz)";
DateTime result;
// Remove the "GMT" portion of the input
input = input.Replace("GMT", "");
if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal, out result))
{
Console.WriteLine($"'{input}' converts to {result} UTC time.");
}
else
{
Console.WriteLine($"'{input}' is not in the correct format.");
}
This example is modified from the ones on the DateTime.TryParseExact documentation.
I am taking selected date from telerik date picker and want to take that selected date and current system time by 24 hours format and want date like this:
For Ex: Current Date = dd/mm/yy HH:Minutes:Seconds
21/1/2016 14:48:21
This is my code:
DateTime dt = DateTime.ParseExact(Datepicker1.SelectedDate.Value.ToShortDateString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);//Error:String was not recognized as a valid DateTime.
Datepicker1.SelectedDate.Value= {1/21/2016 12:00:00 AM}
Datepicker1.SelectedDate.Value.ToShortDateString()=1/21/2016
Error:String was not recognized as a valid DateTime on Datetime.ParseExact.
Change your format in the ParseExact from
"dd/MM/yyyy"
to
"M/d/yyyy" or "M/d/yyyy h:m:s tt" //the first one use .ToShortDateString(), the second one for 1/21/2016 12:00:00 AM
The first one only takes care for case like 21/12/1997
The second one takes care 12/21/1997, 2/21/1997, 12/2/1997, and 2/1/1997 in addition to take care of time info
Also, note that you may consider to have multiple formats (just in case): "d/M/yyyy H:m:s", "d/M/yyyy h:m:s tt" to take care of the case where day and month are swapped and when you have AM/PM.
MSDN link.
#yourDateTime.FormattedReviewDate.ToString("MMM dd,yyyy")
You might even just add a simple property to dateTime for the formatted display:
public string FormattedReviewDate
{
get { return ReviewDate.ToString("MMM dd,yyyy"); }
}
Note: Give your needed format
You don't need to parse anything.
Your Datepicker1.SelectedDate.Value is already DateTime, just assign this value to your dt variable.
DateTime dt = Datepicker1.SelectedDate.Value;
If you want to get it's string representation based on your format, just use ToString method with proper format.
string formattedDate = dt.ToString("dd/M/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
which returns 21/1/2016 14:48:21 as a string.
Also if you want 1/21/2016 as a string, just change your format to M/dd/yyyy like;
string formattedDate = dt.ToString("M/dd/yyyy", CultureInfo.InvariantCulture);
I am getting a string and i want to parse that string as date and want to store it in DataTable.
string can be in formats
1- "2014/23/10"
2- "2014-23-10"
{
string st="2014/23/10";
string st="2014-23-10";
}
And attach time with it.
Any idea to make it possible ?
DateTime.ParseExact or DateTime.TryParseExact are appropriate here - both will accept multiple format strings, which is what you need in this case. Make sure you specify the invariant culture so that no culture-specific settings (such as the default calendar) affect the result:
string[] formats = { "yyyy-MM-dd", "yyyy/MM/dd" };
DateTime date;
if (DateTime.TryParseExact(input, formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
// Add date to the DataTable
}
else
{
// Handle parse failure. If this really shouldn't happen,
// use DateTime.ParseExact instead
}
If the input is from a user (and is therefore "expected" to be potentially broken, without that indicating an error anywhere in the the system), you should use TryParseExact. If a failure to parse indicates a significant problem which should simply abort the current operation, use ParseExact instead (it throws an exception on failure).
Since both are not standart date and time format, you can use DateTime.ParseExact method like;
string st = "2014/23/10";
string st1 = "2014-23-10";
var date = DateTime.ParseExact(st,
"yyyy/dd/MM", CultureInfo.InvariantCulture);
var date1 = DateTime.ParseExact(st1,
"yyyy-dd-MM", CultureInfo.InvariantCulture);
Output will be;
10/23/2014 12:00:00 AM
10/23/2014 12:00:00 AM
Here a demonstration.
Of course these outputs depends your current culture thread.
If you want to format your DateTime's as a string representation, you can use DateTime.ToString(string) overload which accepts as a string format.
Since you have more than one format, you can use DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) overload which is takes your formats as a string array.
var formats = new []{"yyyy-MM-dd", "yyyy/MM/dd"};
DateTime dt;
if(DateTime.TryParseExact(st, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
//
}
else
{
//
}
Convert to a DateTime with DateTime.TryParseExact(); or even DateTime.Parse if you need to be flexible. Then you can format it back out however you like!
See: http://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx
Try
DateTime.Parse(st, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
Set DateTimeStyles based on your requirement.
Try this:
DateTime.Parse(st);
If It the above line not works for you, then add cultrureInfo below:
DateTime.ParseExact(st,"yyyy/dd/MM", CultureInfo.InvariantCulture);
I have a particular loop where DateTime instances are to be generated. My problem is on how does the class interpret the input string.
The incoming input strings are of the format MM/dd/yyyy.
Suppose I have "1/17/2014", DateTime would interpret this as MM/dd/yyyy.
But if I have "6/5/2014", how will I be sure that DateTime will parse this with the format MM/dd/yyyy and not dd/MM/yyyy?
EDIT: Inputs may come with the month and/or day in one- or two-digit format.
Because the dates could come in either MM/dd/yyyy or M/d/yyyy then the overload that takes a string[] is the most appropriate:
var dt = DateTime.ParseExact(input,
new[] { "M/d/yyyy", "MM/dd/yyyy" },
CultureInfo.InvariantCulture,
DateTimeStyles.None);
Now, regardless of the zero-padding it will work as expected.
Use the ParseExact function to specify the format :
DateTime d = DateTime.ParseExact("6/5/2014", "M/d/yyyy", CultureInfo.InvariantCulture);
If your input are in MM/dd/yyyy format, you will get 06/05/2014 instead of 6/5/2014. You will then have to use :
DateTime d = DateTime.ParseExact("06/05/2014", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Be sure of your input format if you don't want to have an exception.
take a look at DateTime.ParseExact, which will allow you to specifically match the string
My application is taking the time now, formatting it into a string, and parsing it back to a valid DateTime value using ParseExact. See below for more details:
DateTime dt = DateTime.Now;
DateTime timeNow = DateTime.Now;
string timeStamp = dt.ToString("MM/dd/yyyy HH:mm:ss");
// To match different countries
if (timeStamp.IndexOf("/") > -1)
{
timeNow = DateTime.ParseExact(timeStamp, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
}
else if (timeStamp.IndexOf(".") > -1)
{
timeNow = DateTime.ParseExact(timeStamp, "MM.dd.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
}
Different countries use different date formats. Is there a way to make my application automatically take into account the different formats, rather than having to make a condition for each one that appears?
Thanks for any help,
Evan
If your application is using a string representation for dates internally, I would suggest using the Sortable format specifier when outputting it. That way, you always know that you can read it back using ParseExact and the "s" format specifier.
The only time you should output dates in any other format is when you need to display them for the user, or when some other program requires them in a particular format.
As #Mike Christensen pointed out in his comment, different locales will interpret dates differently. The default output for many European countries is DD/MM/YYYY, whereas in the U.S. it's usually MM/DD/YYYY. If you take the different locales into account, then there will be ambiguity.
You can pass an array of format specifiers with as many formats as you want to support.
string[] formats = new [] { "MM/dd/yyyy HH:mm:ss", "MM.dd.yyyy HH:mm:ss" };
DateTime d = DateTime.ParseExact
(
timestamp, formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None);
However, since you say you are generating the strings yourself, why don't you just make sure you always format them using the InvariantCulture:
string timestamp = dt.ToString("MM/dd/yyyy HH:mm:ss",
CultureInfo.InvariantCulture);