开发者

How to convert date format to DD-MM-YYYY in C#

开发者 https://www.devze.com 2023-02-04 12:49 出处:网络
How to convert date format to DD-MM-YYYY in C#? I am only looking for DD-开发者_如何转开发MM-YYYY format not anything else.string formatted = date.ToString(\"dd-MM-yyyy\");

How to convert date format to DD-MM-YYYY in C#? I am only looking for DD-开发者_如何转开发MM-YYYY format not anything else.


string formatted = date.ToString("dd-MM-yyyy");

will do it.

Here is a good reference for different formats.


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


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


string formattedDate = yourDate.ToString("dd-MM-yyyy");


DateTime dt = DateTime.Now;

String.Format("{0:dd-MM-yyyy}", dt);


DateTime s1 = System.Convert.ToDateTime(textbox.Trim());
        DateTime date = (s1);
        String frmdt = date.ToString("dd-MM-yyyy"); 

will work


DateTime dt = DateTime.Now;

String.Format("{0:dd-MM-yyyy}", dt);


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


Here we go:

DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "-" + time.Month + "-" + time.Year);

WORKS! :)


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


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


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


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.


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


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.


you could do like this:

return inObj == DBNull.Value ? "" : (Convert.ToDateTime(inObj)).ToString("MM/dd/yyyy").ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消