Dear Sir.

I use following codes to to convert string to date.
In messagebox it displays correct date with format but in textbox it displays only #12:00:00 AM #
How to send date to textbox1 from variable mydate
Please help

Dim mydate As Date
        Dim dateString = "31/12/2009"
        Dim formats As String() = {"dd/MM/yyyy", "dd/MM/yyyy"}
        Dim dateObject As DateTime = DateTime.ParseExact(dateString, formats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.NoCurrentDateDefault)
        MessageBox.Show(dateObject.ToString("dd/MM/yyyy"))
        Me.TextBox1.Text = mydate

Recommended Answers

All 3 Replies

Hi,

If you modify the code with following change,it will work.

Dim mydate As Date
        Dim dateString = "31/12/2009"
        Dim formats As String() = {"dd/MM/yyyy", "dd/MM/yyyy"}
        Dim dateObject As DateTime = DateTime.ParseExact(dateString, formats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.NoCurrentDateDefault)
        MessageBox.Show(dateObject.ToString("dd/MM/yyyy"))
[B]        Me.TextBox1.Text = mydate.ToString()[/B]

But few questions;

1) mydate and dateObject have been used for different purposes?
2) If so, why not format the dateObject also?

Thank you

Hi,

The reason for "mydate" is giving time is because it was not assigned to any date value.

Here is the updated code;

Dim mydate As Date
        Dim dateString = "31/12/2009"
        [B]mydate = DateTime.Now[/B]
        Dim formats As String() = {"dd/MM/yyyy", "dd/MM/yyyy"}
        Dim dateObject As DateTime = DateTime.ParseExact(dateString, formats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.NoCurrentDateDefault)
        MessageBox.Show(dateObject.ToString("dd/MM/yyyy"))
        [B]Me.TextBox1.Text = mydate.ToString("dd/MM/yyyy")[/B]

Let us know if this helps.

Thank you.

i dont really understand.

me.textbox.text = formatdatetime$(now(), dateformat.shortdate)

prossible dateformats: shortdate, longdate, generaldate, longtime, shorttime.

another method i use:

vb6.format$(now(), "YYYYMMDD")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.