Does the date conversion always remain in en-BR?

Asked

Viewed 2,085 times

7

I’m trying to convert a date in Asp.Net MVC to yyyy-MM-dd, what happens is that always when I convert and display this date it gets dd/MM/yyyy and I don’t know why it happens.

How to solve this?

String d1 = `2019-03-01`;
DateTime d = DateTime.Parse(d1);
Debug.Writeline(d);

Resultado: saida 01/03/2019

4 answers

11

As I said before here and here:

Dates have no format

A date is just a concept, an idea: it represents a specific point in the calendar.

The date of "1 March 2019" represents this: the specific point of the calendar, which corresponds to the 1st of March of the year 2019. To express this idea in text form, I can write it in different ways:

  • 01/03/2019 (a common format in many countries, including Brazil)
  • 3/1/2019 (American format, reversing day and month)
  • 2019-03-01 (the format ISO 8601)
  • 1st of March 2019 (in good Portuguese)
  • March 1st, 2019 (in English)
  • 2019 年 3 月 1 日 (in Japanese)
  • and many others...

Note that each of the above formats is different, but all represent the same date (the same numerical values of the day, month and year).


That said, the string 2019-03-01 is just a text that represents the date in a specific format. When you turn this string into a DateTime, is creating an object that contains the respective values (day 1, month 3, year 2019). But the date itself is not in a specific format.

When you print the date, of course, it has to be shown in some format. Methods like WriteLine, on receiving a DateTime, use a default format as per CurrentCulture (see detailed explanation here).

If you want to use a specific format without relying on CurrentCulture (or without needing to change it), use ToString(formato). Example:

d.ToString("yyyy-MM-dd") // retorna 2019-03-01

Remembering that this method returns a string: a representation of the date in a specific format, not the date (the DateTime) in itself.

  • 1

    Great answer! I was going to improve mine, but this one is perfect.

10


This happens because you are not formatting the output. A date has no format, what is formatted is representation of this date in string.

It is possible to set the format of a date using the . tostring method()

Debug.Writeline(d.ToString("yyyy-MM-dd"));

In this case, the culture won’t make any difference, so I omitted it. In general it is always good to use it, even for reasons of custom and/ or maintenance of code (if someone changes the format, etc.).

  • 2

    +1 for being the only answer - besides my :-) - to point out that dates have no format (something that a lot of people get confused when manipulating dates)

5

Try to do:

string d1 = DateTime.Now.ToString("yyyy-MM-dd");

Or else if that’s the case:

d1.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.GetCultureInfo("pt-BR"));

0

You can make explicit the date format you want on output using Parseexact():

public Static Datetime Parseexact (string s, string format, Iformatprovider Provider);

More information:

Datetime.Parseexact Method

CultureInfo culture = new CultureInfo("en-US");
string d1 = "2019-03-01";
string format = "yyyy-MM-dd";

DateTime dateTime = DateTime.ParseExact(d1, format, culture);

  • I had already tried the DateTime.ParseExact but keeps showing off dd/MM/yyyy

  • 2

    Your answer is wrong. AP is looking for something else.

  • It is because you are only looking for the date display format in the output and not in the input. I believe the answers you have given then will suit you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.