Converting string format to datetime in mm/dd/yyyy
You are looking for the DateTime.Parse()
method (MSDN Article)
So you can do:
var dateTime = DateTime.Parse("01/01/2001");
Which will give you a DateTime
typed object.
If you need to specify which date format you want to use, you would use DateTime.ParseExact
(MSDN Article)
Which you would use in a situation like this (Where you are using a British style date format):
string[] formats= { "dd/MM/yyyy" }
var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
Convert string into mm/dd/yyyy format
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.
How to convert dd/MM/YYYY formatted string date to YYYY-MM-dd datetime?
You parse the string with date in wrong way.
You should:
DateTime dt = DateTime.ParseExact("04/26/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("yyyy-MM-dd"));
Converting dd/mm/yyyy formatted string to Datetime
You need to use DateTime.ParseExact
with format "dd/MM/yyyy"
DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Its safer if you use d/M/yyyy
for the format, since that will handle both single digit and double digits day/month. But that really depends if you are expecting single/double digit values.
Your date format day/Month/Year
might be an acceptable date format for some cultures. For example for Canadian Culture en-CA
DateTime.Parse
would work like:
DateTime dt = DateTime.Parse("24/01/2013", new CultureInfo("en-CA"));
Or
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
DateTime dt = DateTime.Parse("24/01/2013"); //uses the current Thread's culture
Both the above lines would work because the the string's format is acceptable for en-CA
culture. Since you are not supplying any culture to your DateTime.Parse
call, your current culture is used for parsing which doesn't support the date format. Read more about it at DateTime.Parse.
Another method for parsing is using DateTime.TryParseExact
DateTime dt;
if (DateTime.TryParseExact("24/01/2013",
"d/M/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
//valid date
}
else
{
//invalid date
}
The TryParse
group of methods in .Net framework doesn't throw exception on invalid values, instead they return a bool
value indicating success or failure in parsing.
Notice that I have used single d
and M
for day and month respectively. Single d
and M
works for both single/double digits day and month. So for the format d/M/yyyy
valid values could be:
- "24/01/2013"
- "24/1/2013"
- "4/12/2013" //4 December 2013
- "04/12/2013"
For further reading you should see: Custom Date and Time Format Strings
Convert string to mm/dd/yyyy datetime
If the string is expressed in the format MM/dd/yyyy then
CultureInfo us = new CultureInfo("en-US");
dtAssemblyDate = DateTime.ParseExact(txtOperationSignatureDate.Value, "MM/dd/yyyy", us);
but I prefer to use DateTime.TryParse to avoid surprises...
if(DateTime.TryParse(txtOperationSignatureDate.Value,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dtAssemblyDate))
Console.WriteLine(dtAssemblyDate.ToShortDateString());
How to convert string date (25.04.2016) to date format (mm/dd/yyyy)?
Use ParseExact with the format you need to get the string into a date variable, then .ToString to convert to another format:
Dim dateString As String = "25.05.2016"
Dim dateFormat As String = "dd.MM.yyyy"
Dim dateValue = DateTime.ParseExact(dateString, dateFormat, CultureInfo.InvariantCulture)
Debug.WriteLine(dateValue.ToString("MM/dd/yyyy"))
The list of custom datetime formats can be found here
Convert DateTime in mm/dd/yyyy to DateTime in yyyy-dd-mm
Saying I want to convert date time in some format to date time to some other format is technically wrong because the format is about string representation of DateTime
.
Why can't I convert a string in yyyy-dd-MM
format to DateTime
using DateTime.Parse()
?
The
DateTime.Parse
parses a date and time string by using the conventions of the current culture.To parse a date and time string in a custom/fixed format across machine and possibly cultural boundaries, you can use
DateTime.ParseExact
.
So to convert a string in yyyy-dd-MM
to DateTime
you can use:
var d = DateTime.ParseExact("2016-31-12", "yyyy-dd-MM", CultureInfo.InvariantCulture);
Also as mentioned in MSDN, because the string representation of a date and time must conform to a recognized pattern, you should always use exception handling when calling the parse method to parse user input or consider using TryParse
or TryParseExact
.
More Information:
- DateTime.Parse
Related Topics
How to Bring Up the Built-In File Copy Dialog
A Dictionary Object That Uses Ranges of Values for Keys
Overloaded Method-Group Argument Confuses Overload Resolution
ASP.NET MVC Route to Catch All *.Aspx Requests
Refactoring Code to Avoid Anti-Pattern
Could Not Load File or Assembly 'System.Web.Http 4.0.0 After Update from 2012 to 2013
In C# Differencebetween a Destructor and a Finalize Method in a Class
Passing Just a Type as a Parameter in C#
How to Check If Int Is Legal Enum in C#
How to Read/Stream a File Without Loading the Entire File into Memory
Windows Forms Graphic Issue on Windows 10 Os
Trying to Debug Windows Store App from Dump Files
When Would You Use Delegates in C#
Event Signature in .Net -- Using a Strong Typed 'Sender'
Dbarithmeticexpression Arguments Must Have a Numeric Common Type