9

I want to convert DateTime to String.

check the below code.

namespace TestDateConvertion
{
    class Program
    {
        static void Main(string[] args)
        {
            object value = new DateTime(2003,12,23,6,22,30);
            DateTime result = (DateTime)value;
            Console.WriteLine(result.ToString("dd/MM/yyyy"));
            Console.ReadLine();
        }
    }
}

I have changed my system date format to Faeroese.

and I am getting the output as

23-12-2013

How should I get the output as ?

23/12/2013

And consider this another scenario, suppose, i have a Customculture Info , and i want to convert my date w.r.t my custom culture, what i was doing before was as follows,

string.Format(customCulture, "{0:G}", result);

now how shall i get the datetime in string using customCulture and it should not depend on system DateTime?

Nomesh Gajare
  • 855
  • 2
  • 12
  • 28
  • try this http://stackoverflow.com/questions/6362088/c-sharp-date-formatting-is-losing-slash-separators – Mahmoud Darwish Jul 08 '13 at 12:12
  • Console.WriteLine(DateTime.Now.ToString("ddd M/dd/yy", CultureInfo.InvariantCulture)); Console.ReadLine(); from http://stackoverflow.com/questions/6362088/c-sharp-date-formatting-is-losing-slash-separators – Md. Parvez Alam Jul 08 '13 at 12:13
  • 5
    `/` has a special meaning of "replace me with the current culture's date separator". You can enforce it with passing `CultureInfo.InvariantCulture` as second parameter. – Tim Schmelter Jul 08 '13 at 12:15
  • There was an answer from user oleksiiless [which seems to be correct](http://ideone.com/ab37qm) (AFAIKT). But it was downvoted and i assume this was the reason for the author to delete it. Could someone take a look if i set the culture in the right way to verify that `now.ToShortDateString()` is enough to ouptput the date as required? – surfmuggle Jul 08 '13 at 12:42

7 Answers7

14

Looks like your culture's date separator is - and as Tim pointed, / replaces itself with it.

You should use CultureInfo.InvariantCulture as a second parameter in your result.ToString() method.

Gets the CultureInfo object that is culture-independent (invariant).

object value = new DateTime(2003, 12, 23, 6, 22, 30);
DateTime result = (DateTime)value;
Console.WriteLine(result.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));

Output will be;

23/12/2003

Here a DEMO.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
4

Try this one

Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
1

You need to add this

Console.WriteLine(result.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));

Now your Code becomes

object value = new DateTime(2003, 12, 23, 6, 22, 30);
DateTime result = (DateTime)value;
Console.WriteLine(result.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
Console.ReadLine();

NOTE *Add using System.Globalization;*

Usman
  • 3,200
  • 3
  • 28
  • 47
0

You could use the invariant culture:

Console.WriteLine(
    result.ToString("dd/MM/yyyy", 
    System.Globalization.CultureInfo.InvariantCulture
);
JDunkerley
  • 12,355
  • 5
  • 41
  • 45
0

try

string.Format("{0:dd/MM/yyyy}",result)

Good luck

Wing

wingyip
  • 3,465
  • 2
  • 34
  • 52
0

I fully agree with Tim Schmelter's comment and Soner Gönül's answer. Just wanted to add that when you are using Date Time format you should specify culture because by default culture will be get from Thread.CurrentThread.CurrentCulture (culture set in Control Panel->Region and Languages->Format), that means that with different settings your input will be different.

Take a look on your example with different cultures:

object value = new DateTime(2003, 12, 23, 6, 22, 30);
DateTime result = (DateTime)value;
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
       Console.WriteLine(result.ToString("dd/MM/yyyy", culture));    
}
Oleksii Aza
  • 5,368
  • 28
  • 35
0

'/' is a special char that means "regional settings date separator". if you want use it like a normal char you can quote it using the quoter chart '\' Example:

DateTime.Now.ToString(@"dd\/MM\/yyyy")
stefano m
  • 4,094
  • 5
  • 28
  • 27