How to convert date in dd / mm / yyyy format?

1

Once I published my app, I started getting the dates in American format like this: 9/14/2016 12:00:00 AM

How to format for dd/MM/yyyy ?

I tried convert.ToDateTime() more does not work.

    
asked by anonymous 14.09.2016 / 18:11

3 answers

3

try the following:

DataLabel.Text = variavelDateTime.ToString("dd/MM/yyyy HH:mm:ss");

For more information, you can view this Microsoft page: link

    
14.09.2016 / 18:39
3

If you are sure of the form, one of the ways to do this would be to do a Parse() " in the American format:

DateTime.Parse(data, new CultureInfo("en-US"));

If it can fail and you want to specify the format you can use the TryParseExact() :

DateTime.TryParseExact(data, "M/d/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture,
                                                            DateTimeStyles.None, out date2)

So if you fail, you can handle it somehow. If that works, but you're sure that the conversion will always work, you can use the ParseExact() that is simpler.

To display in a specific format you can use ToString() in most cases. But there are other options, so it's always good to know all the documentation.

If the format did not respond, you can study all available patterns to> and adapt.

See working on dotNetFiddle .

In C # 7 you can make it simpler:

DateTime.TryParseExact(data, "M/d/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture,
    DateTimeStyles.None, out var date2) //note o var, a variável foi declarada aqui mesmo
    
15.09.2016 / 00:04
0

You can use Datetime.Parse

var dt = DateTime.Parse("2016-05-08 04:00:00 PM").ToString("dd-MM-yyyy HH:mm:ss");
    
14.09.2016 / 18:42