saurav2109
saurav2109

Reputation: 293

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.

Upvotes: 26

Views: 330170

Answers (16)

Kummari Rajesh
Kummari Rajesh

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

you could do like this:

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

Upvotes: -1

abhilash
abhilash

Reputation: 31

DateTime dt = DateTime.Now;

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

Upvotes: 3

Mahesh
Mahesh

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

GeorgDangl
GeorgDangl

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

sweta
sweta

Reputation: 31

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

will work

Upvotes: 3

Husni Salax
Husni Salax

Reputation: 2020

Here we go:

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

WORKS! :)

Upvotes: 1

Antonio Correia
Antonio Correia

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

Brian Evans
Brian Evans

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

DareDevil
DareDevil

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

Euphoric
Euphoric

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

user528573
user528573

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

evilone
evilone

Reputation: 22740

DateTime dt = DateTime.Now;

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

Upvotes: 6

Teekin
Teekin

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

alexn
alexn

Reputation: 58962

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

will do it.

Here is a good reference for different formats.

Upvotes: 55

Anthony Pegram
Anthony Pegram

Reputation: 126834

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

Upvotes: 8

Related Questions