问题
How to convert date format to DD-MM-YYYY in C#? I am only looking for DD-MM-YYYY format not anything else.
回答1:
string formatted = date.ToString("dd-MM-yyyy");
will do it.
Here is a good reference for different formats.
回答2:
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);
回答3:
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
回答4:
string formattedDate = yourDate.ToString("dd-MM-yyyy");
回答5:
DateTime dt = DateTime.Now;
String.Format("{0:dd-MM-yyyy}", dt);
回答6:
DateTime s1 = System.Convert.ToDateTime(textbox.Trim());
DateTime date = (s1);
String frmdt = date.ToString("dd-MM-yyyy");
will work
回答7:
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");
回答8:
DateTime dt = DateTime.Now;
String.Format("{0:dd-MM-yyyy}", dt);
回答9:
Here we go:
DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "-" + time.Month + "-" + time.Year);
WORKS! :)
回答10:
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}";
回答11:
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
回答12:
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...
回答13:
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 likestring date = variableConvertedToDate.ToString("dd-MM-yyyy")
orstring date = variableConvertedToDate.ToShortDateString() in this case result is dd/MM/yyyy.
回答14:
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.
回答15:
you could do like this:
return inObj == DBNull.Value ? "" : (Convert.ToDateTime(inObj)).ToString("MM/dd/yyyy").ToString();
来源:https://stackoverflow.com/questions/4649639/how-to-convert-date-format-to-dd-mm-yyyy-in-c-sharp