26

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

halfer
  • 19,824
  • 17
  • 99
  • 186
saurav2109
  • 293
  • 1
  • 3
  • 3
  • Are you converting a string to another string? Do you want to format a DateTime as a string with the `DD-MM-YYYY` format? Which language: VB, C#, something else? Please provide more info. – matt Jan 10 '11 at 17:38
  • 1
    Be aware that the ToString("dd-MM-yyyy") formatting is case sensitive. If you use lower-case M's, you will not get month, something I have stumbled over too many times to count. – DOK Jan 10 '11 at 17:41
  • Please follow this link [DateTime Format](https://stackoverflow.com/questions/3074671/how-to-change-date-format-from-dd-mm-yyyy-or-mm-dd-yyyy-to-yyyy-mm-dd) – Aikansh Mann May 18 '19 at 10:10

16 Answers16

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

will do it.

Here is a good reference for different formats.

alexn
  • 57,867
  • 14
  • 111
  • 145
  • 1
    Thanks for the quick response, but its not working..Below error i am getting..cannot convert from 'string' to 'System.IFormatProvider' – saurav2109 Jan 10 '11 at 18:09
  • How are you calling it? Doing exactly what i wrote will work. See MSDN for more info http://msdn.microsoft.com/en-US/library/zdtaw1bw%28v=VS.100%29.aspx – alexn Jan 10 '11 at 18:12
  • I am selecting Start Date and End date from the below query after that i am using for loop for all the selected value...Please have a look below. hrms_job = hrms_job.Append("SELECT [Job Code], [Job Name],[Start Date], [End Date] FROM [hrms_job$]"); – saurav2109 Jan 10 '11 at 18:14
  • for (int iRowCount = 0; iRowCount < hrms_jobdata.Tables[0].Rows.Count; iRowCount++) {JobCode = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Code"].ToString();JobName = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Name"].ToString();StartDate = hrms_jobdata.Tables[0].Rows[iRowCount]["Start Date"].ToString(); EndDate = hrms_jobdata.Tables[0].Rows[iRowCount]["End Date"].ToString(); JobCodeList.Add(JobCode + JobName); } here only after For loop I want to format the start date and end date to DD-MM-YYYY format – saurav2109 Jan 10 '11 at 18:19
  • 1
    @saurav2109 : You are confusing String and DateTime types. You must first parse your date in string into DateTime and then use ToString with formater to get desired result. – Euphoric Jan 10 '11 at 18:20
  • case matters... its 'yyyy' not 'YYYY'. – Jaster Nov 16 '15 at 12:59
13

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);
Teekin
  • 12,581
  • 15
  • 55
  • 67
  • Thanks a lot for the reply but this is not working, string.format will not give us DD-MM-YYYY format, it will give DD/MM/YYYY format – saurav2109 Jan 10 '11 at 18:06
12

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

DareDevil
  • 5,249
  • 6
  • 50
  • 88
8
string formattedDate = yourDate.ToString("dd-MM-yyyy");
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • Thanks a lot for quick responsecannot but its not working.. convert from 'string' to 'System.IFormatProvider' error I am getting – saurav2109 Jan 10 '11 at 18:08
5
DateTime dt = DateTime.Now;

String.Format("{0:dd-MM-yyyy}", dt);
evilone
  • 22,410
  • 7
  • 80
  • 107
3
DateTime s1 = System.Convert.ToDateTime(textbox.Trim());
        DateTime date = (s1);
        String frmdt = date.ToString("dd-MM-yyyy"); 

will work

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
sweta
  • 31
  • 1
3
DateTime dt = DateTime.Now;

String.Format("{0:dd-MM-yyyy}", dt);
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
abhilash
  • 31
  • 1
2

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");
Euphoric
  • 12,645
  • 1
  • 30
  • 44
  • hrms_job = hrms_job.Append("SELECT [Job Code], [Job Name],[Start Date], [End Date] FROM [hrms_job$]");for (int iRowCount = 0; iRowCount < hrms_jobdata.Tables[0].Rows.Count; iRowCount++){JobCode = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Code"].ToString();JobName = hrms_jobdata.Tables[0].Rows[iRowCount]["Job Name"].ToString();StartDate = hrms_jobdata.Tables[0].Rows[iRowCount]["Start Date"].ToString();EndDate = hrms_jobdata.Tables[0].Rows[iRowCount]["End Date"].ToString();DateTime converteddate = DateTime.Parse(StartDate); string StartDateFormated = converteddate.ToString("DD-MM-YYYY");} – saurav2109 Jan 10 '11 at 18:36
  • Below is my code, sorry for disturbing you again, but I am not geeting exactly what I want..After your suggestion now I am getting DD-02-YYYY, means i am getting month value but not all. Please help – saurav2109 Jan 10 '11 at 18:37
  • Thhanks a lot Everyone..I got the solution..I was doing DD-MM-YYYY but actually it was dd-MM-yyyy that is case sensitive..Thanks a lot to all for all your nice support – saurav2109 Jan 10 '11 at 18:41
1

Here we go:

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

WORKS! :)

Husni Salax
  • 1,968
  • 1
  • 19
  • 29
1

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}";
GeorgDangl
  • 2,146
  • 1
  • 29
  • 37
0

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

Brian Evans
  • 235
  • 1
  • 4
  • 21
0

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.

Antonio Correia
  • 1,093
  • 1
  • 15
  • 22
0

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

user528573
  • 143
  • 1
  • 6
0
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); 
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
-1
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.

yaakov
  • 5,552
  • 35
  • 48
Mahesh
  • 157
  • 2
  • 8
-1

you could do like this:

return inObj == DBNull.Value ? "" : (Convert.ToDateTime(inObj)).ToString("MM/dd/yyyy").ToString();
Antonio Correia
  • 1,093
  • 1
  • 15
  • 22