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?
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.
Try this one
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
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;*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With