Format Date

nvhung.mctt

Member
Joined
Nov 4, 2009
Messages
12
Location
vietnam
Programming Experience
1-3
Dear All,

I have a code below

VB.NET:
TxtDate_0.Text = Format(StartDate, "dd-mm-yyyy hh:nn")
TxtDate_1.Text = Format(EndDate, "dd-mm-yyyy hh:nn")

for example: StartDate = 1/1/2009
EndDate = 31/12/2009

Normal, I have to result is:
TxtDate_0.text = 01-Jan-2009 00:00
TxtDate_1.text = 31-Dec-2009 00:00

but with code above, result is not correct

Please show me code correct.

Thanks very much.

Best Regards,
Nguyen Van Hung
 
What is not correct about it?
VB.NET:
Dim StartDate As Date
StartDate = CDate(TxtDate_0.Text)
TxtDate_0.Text = Format(TxtDate_0.Text, "dd-mm-yyyy hh:nn").ToString
 
Assuming StartDate is a Date or a DateTime object (which it should be):
VB.NET:
StartDate.ToString("dd-MMM-yyyy")
'Produces: 17-NOV-2009
 
Dear All,

This is code for my function

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StartDate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Date
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] EndDate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Date
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] iYr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]iYr = Year(Now)
StartDate = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"1/1/"[/COLOR][/SIZE][SIZE=2] + Str$(iYr))
EndDate = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"12/31/"[/COLOR][/SIZE][SIZE=2] + Str$(iYr))
TxtDate_0.Text = StartDate.ToString([/SIZE][SIZE=2][COLOR=#800000]"dd-mmm-yyyy"[/COLOR][/SIZE][SIZE=2])
TxtDate_1.Text = EndDate.ToString([/SIZE][SIZE=2][COLOR=#800000]"dd-mmm-yyyy"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] 
[/SIZE]

I still did not work well with Vb.net but it works well with vb6(on together computer)

code vb6

Dim StartDate as Date
Dim EndDate as date
Dim iYr as integer

iYr = Year(Now)
StartDate = CDate("1/1/" + Str$(iYr))
EndDate = CDate("12/31/" + Str$(iYr))
txtDate_0 = Format(StartDate, "dd-mmm-yyyy hh:nn")
txtDate_1 = Format(EndDate, "dd-mmm-yyyy hh:nn")

this is result in vb.net

Pic.JPG

Thansk for your support.

Best Regards,
Nguyen Van Hung
 
Dear All,

This is code for my function

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StartDate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Date
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] EndDate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Date
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] iYr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]iYr = Year(Now)
StartDate = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"1/1/"[/COLOR][/SIZE][SIZE=2] + Str$(iYr))
EndDate = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"12/31/"[/COLOR][/SIZE][SIZE=2] + Str$(iYr))
TxtDate_0.Text = StartDate.ToString([/SIZE][SIZE=2][COLOR=#800000]"dd-mmm-yyyy"[/COLOR][/SIZE][SIZE=2])
TxtDate_1.Text = EndDate.ToString([/SIZE][SIZE=2][COLOR=#800000]"dd-mmm-yyyy"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] 
[/SIZE]

I still did not work well with Vb.net but it works well with vb6(on together computer)

code vb6

Dim StartDate as Date
Dim EndDate as date
Dim iYr as integer

iYr = Year(Now)
StartDate = CDate("1/1/" + Str$(iYr))
EndDate = CDate("12/31/" + Str$(iYr))
txtDate_0 = Format(StartDate, "dd-mmm-yyyy hh:nn")
txtDate_1 = Format(EndDate, "dd-mmm-yyyy hh:nn")

this is result in vb.net

Pic.JPG

Thansk for your support.

Best Regards,
Nguyen Van Hung
You can simplify this quite a bit and make it much more readable as well.
VB.NET:
Dim StartDate As Date = CDate("1/1/" & DateTime.Now.Year.ToString)
Dim EndDate As Date = CDate("12/31/" & DateTime.Now.Year.ToString)

txtDate_0 = StartDate.ToString("dd-MMM-yyyy hh:nn")
txtDate_1 = EndDate.ToString("dd-MMM-yyyy hh:nn")
 
Back
Top