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

How to convert date format to DD-MM-YYYY in C#? I am only looking for DD-MM-YYYY format not anything else.

This question is related to c# .net datetime-format

The answer is


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


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


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

DateTime dt = DateTime.Now;

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

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.


DateTime dt = DateTime.Now;

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

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

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.


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

will do it.

Here is a good reference for different formats.


Here we go:

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

WORKS! :)


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

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

you could do like this:

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

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

will work


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


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to datetime-format

How do I convert 2018-04-10T04:00:00.000Z string to DateTime? Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8) How can I create basic timestamps or dates? (Python 3.4) Format Instant to String Laravel $q->where() between dates How to Convert datetime value to yyyymmddhhmmss in SQL server? AngularJS - convert dates in controller Display DateTime value in dd/mm/yyyy format in Asp.NET MVC Display date in dd/mm/yyyy format in vb.net Convert DataFrame column type from string to datetime, dd/mm/yyyy format