How do I chane the date format? VB.net or VS 2005(VB)

Hi

I am getting the date and time to a textbox1.text as 12/03/2007 18:50:49. I am using printpreviewdialog1 to get this value to report to printout. It works fine.

But my question is how do I change the date formate of textbox1.text to 12-03-2007 18:50:49 in the field of printpreviewdialog1. I am able to get the date as it in the textbox1.text but could not change the formate of it.

Advance Thanks

Vaish

[437 byte] By [Vaish] at [2007-12-31]
# 1

Are you using a DateTime object to store/get the date?

If so, try using the DateTime.ToString() functions. There are several versions of ToString() that can format it for you.

If you want to get a String into a DateTime instead, look at DateTime.TryParse() which converts a String to a DateTime.

Look on the MSDN help for details of these two functions.

Regards

Jero

JeroGrav at 2007-9-7 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Try as follow:

formatdatetime(now,2)

  • Best Regards!
GavinJin-MSFT at 2007-9-7 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Hi

Somehow I managed to work out but need bit more help

ex; TextBox1.Text = "14/03/2007T14:08:23"

Dim TestDate As Date = TextBox1.Text

Dim DoI_ans As String = (TestDate.Date.Day() & "-" & TestDate.Date.Month() & "-" & TestDate.Date.Year() & " & " & TestDate.Hour() & ":" & TestDate.Minute() & ":" & TestDate.Second())

I am getting the output like this : 14 - 3 - 2007 & 14: 8 :23 but I want every thing in double digits like below
| |
| |
v v

14 - 03 - 2007 & 14:08:23

Thanks

Vaish

Vaish at 2007-9-7 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

hi,Vaish

I have tied a code like this

Imports System

Imports System.Globalization

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.DateTimePicker1.Format = DateTimePickerFormat.Custom

Me.DateTimePicker1.CustomFormat = "dd-MM-yyyy hh:mm:ss"

Me.TextBox1.Text = Me.DateTimePicker1.Text

Dim dts As New DateTime

Dim culture = New CultureInfo("fr-FR", True)

Dim string1 As String = Me.TextBox1.Text

Try

dts = DateTime.Parse(string1, culture, DateTimeStyles.NoCurrentDateDefault)

Catch

MessageBox.Show("The datetime type is not correct", "Wrong!")

End Try

Dim mytf As String = "dd/MM/yyyy hh:mm:ss"

Dim s As String

s = dts.ToString(mytf, DateTimeFormatInfo.InvariantInfo)

Me.TextBox3.Text = s

End Sub

End Class

In this methed,I usse datatime.parse to convert string type.I hope it helps.more more information ,look at it in msdn.

GavinJin-MSFT at 2007-9-7 > top of Msdn Tech,Windows Forms,Windows Forms General...