1

Just started upgrading to VB.Net from VB6.

txtFrom.Text = "1/8"
txtFrom.Text = Format(txtFrom.Text, "dd/MM/yyyy")

This code produces DD/MM/YYYY in the text box.

What should I do to produce 01/08/13?

1
  • 2
    Without year in input which year you expect in output? Commented Sep 29, 2013 at 19:39

4 Answers 4

5

Parse the date. Reformat the date.

Dim d As Date = Date.ParseExact(txtFrom.Text, "d/M", CultureInfo.InvariantCulture)
txtFrom.Text = d.ToString("dd/MM/yy")

Custom date format strings

Sign up to request clarification or add additional context in comments.

2 Comments

Won't this create a date with a 0 (epoch-based) for the year value? So, January 8, 0001 A.D.? I expect he'll need another piece to put the current or prior year in there.
@JoelCoehoorn: I tested it a little while afterwards, and apparently not.
1

The format function with "dd/MM/yyyy" will format a date object, not a string. You can convert the string from the textbox to a date using CDate:

txtFrom.Text = Format(CDate(txtFrom.Text), "dd/MM/yyyy")

You might want to use IsDate or a Try-Catch block in case txtFrom.Text is an invalid date.

Comments

0

What should I do to produce 01/08/13?

dd/MM/yy

Comments

-2
Dim sFormat As System.Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo()
sFormat.ShortDatePattern = "dd/MM/yyyy"
txtFrom.Text = Format(Convert.ToDateTime(txtFrom.Text+Now.Year.ToString(), sFormat), "dd/MM/yyyy")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.