How to work with dates in the dd/mm/yyyy format in Vb.net?

Asked

Viewed 8,837 times

2

I have a text box named txt_cadastro.Text. When using the following command:

txt_cadastro.Text = DateString

Man textbox is completed in the following format: 04-23-2014 What do I do for it to be presented in the Brazilian format dd/mm/yyyy, in the case: 23-04-2014?

I looked into it and I saw that I should change the currentculture to the right format, but I couldn’t do it!

Using the following command: txt_cadastro.Text = Format(DateString, "dd/MM/yyyy") the text box is filled with dd/MM/yyyy instead of 23-04-2014

Any simpler suggestion of date formatting?

  • 2

    The problem is in VB or VB.Net?

3 answers

5


If the idea is to show today’s date, try it like this:

txt_cadastro.Text = CDate(Now).ToString("dd/MM/yyyy")
  • Ai broke everything! I wrote exactly the way you said: 'txt_cadastre.Text =Cdate(Datestring). Tostring("dd/MM/yyyy") txt_nascimento.Text = "08/10/1989"' Did not work and the other box was no longer filled!

  • How you’re filling the Datestring?

  • I put it exactly as I said... I should replace Datestring with something else??

  • 1

    edited the answer. I imagined that Datestring was already worth

  • aaaaaaaaaaaaaaeeeeeeeeeee I am jumping for joy here. Thank you very much!!

  • 1

    @user7854 if it has already confirmed that it has resolved, could give its positive vote to the Fnightangel, and after some time give a Accept in response.

Show 1 more comment

1

While the accepted solution will work, the literal format conversion approach will cast the application if needed in the future to make the presentation more flexible (as preferred by the user).

Also, depending on the size of the application, it can generate considerable code Boilerplate and repetitive (to enforce all cases).


The most correct would be to use the current user culture, as mentioned in the question. Or else at least force it to use the necessary culture.

In case I force you, I’d be like this:

' Na inicialização da sua aplicação, force a cultura padrão conforme necessidade 
' (neste caso, Português/Brasil)
Dim culturaPtBr = System.Globalization.CultureInfo.GetCultureInfo("pt-BR")
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = culturaPtBr
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = culturaPtBr

' Na apresentação de valores em tela
' Converte a data para o formato "curto", que na cultura Português (Brasil) é: dd/mm/aaaa
minhaData.ToString("d")

0

txt_cadastre.Text = Today.Tostring("dd/MM/yyyy")

Browser other questions tagged

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