Reputation: 293
How to convert date format to DD-MM-YYYY in C#? I am only looking for DD-MM-YYYY format not anything else.
Upvotes: 26
Views: 330170
Reputation: 1
dateString = "not a date";
// Exception: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
DateTime dateTime11; // 1/1/0001 12:00:00 AM
bool isSuccess2 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime11);
Upvotes: 0
Reputation: 303
you could do like this:
return inObj == DBNull.Value ? "" : (Convert.ToDateTime(inObj)).ToString("MM/dd/yyyy").ToString();
Upvotes: -1
Reputation: 167
var dateTimeString = "21-10-2014 15:40:30";
dateTimeString = Regex.Replace(dateTimeString, @"[^\u0000-\u007F]", string.Empty);
var inputFormat = "dd-MM-yyyy HH:mm:ss";
var outputFormat = "yyyy-MM-dd HH:mm:ss";
var dateTime = DateTime.ParseExact(dateTimeString, inputFormat, CultureInfo.InvariantCulture);
var output = dateTime.ToString(outputFormat);
Console.WriteLine(output);
Try this, it works for me.
Upvotes: -1
Reputation: 2192
From C# 6.0 onwards (Visual Studio 2015 and newer), you can simply use an interpolated string with formatting:
var date = new DateTime(2017, 8, 3);
var formattedDate = $"{date:dd-MM-yyyy}";
Upvotes: 1
Reputation: 31
DateTime s1 = System.Convert.ToDateTime(textbox.Trim());
DateTime date = (s1);
String frmdt = date.ToString("dd-MM-yyyy");
will work
Upvotes: 3
Reputation: 2020
Here we go:
DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "-" + time.Month + "-" + time.Year);
WORKS! :)
Upvotes: 1
Reputation: 1091
The problem is that you're trying to convert a string, so first you should cast your variable to date and after that apply something like
string date = variableConvertedToDate.ToString("dd-MM-yyyy")
or
string date = variableConvertedToDate.ToShortDateString()
in this case result is dd/MM/yyyy.
Upvotes: 0
Reputation: 235
I ran into the same issue. What I needed to do was add a reference at the top of the class and change the CultureInfo of the thread that is currently executing.
using System.Threading;
string cultureName = "fr-CA";
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
DateTime theDate = new DateTime(2015, 11, 06);
theDate.ToString("g");
Console.WriteLine(theDate);
All you have to do is change the culture name, for example: "en-US" = United States "fr-FR" = French-speaking France "fr-CA" = French-speaking Canada etc...
Upvotes: 0
Reputation: 5349
Here is the Simplest Method.
This is the String value: "5/13/2012"
DateTime _date;
string day = "";
_date = DateTime.Parse("5/13/2012");
day = _date.ToString("dd-MMM-yyyy");
It will output as: 13-May-2012
Upvotes: 12
Reputation: 12849
First convert your string into DateTime variable:
DateTime date = DateTime.Parse(your variable);
Then convert this variable back to string in correct format:
String dateInString = date.ToString("dd-MM-yyyy");
Upvotes: 2
Reputation: 143
Do you have your date variable stored as a String or a Date type?
In which case you will need to do something like
DateTime myDate = null;
DateTime.TryParse(myString,myDate);
or
Convert.ToDateTime(myString);
You can then call ToString("dd-MM-yyyy") on your date variable
Upvotes: 0
Reputation: 13279
According to one of the first Google search hits: http://www.csharp-examples.net/string-format-datetime/
// Where 'dt' is the DateTime object...
String.Format("{0:dd-MM-yyyy}", dt);
Upvotes: 13