Date Subtraction/Formatting Issues

D

David Doing

This worked in VB6, but does not in VB.Net

Format(MyDate - DateSerial(Year(MyDate) - 1, 12, 31), "000")

Where:
MyDate is type Date and = #11/16/2003#

It should return the Julian Day # --> 321

But I get an error "Specified cast is not allowed"

Any help here??

Thanks,
David
 
A

Armin Zingler

David Doing said:
This worked in VB6, but does not in VB.Net

Format(MyDate - DateSerial(Year(MyDate) - 1, 12, 31), "000")

Where:
MyDate is type Date and = #11/16/2003#

It should return the Julian Day # --> 321

But I get an error "Specified cast is not allowed"

Any help here??

(MyDate.Subtract(New Date(MyDate.Year, 12, 31))).ToString("000")


Maybe you like the shorter version: ;-)

MyDate.DayOfYear.ToString("000")
 
P

Peter Huang

Hi David,

You may try to use the code below.
[VB.NET]
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command1.Click
Dim ss As String
Dim MyDate As Date
MyDate = #11/16/2003#
ss = Format(DateTime.FromOADate(MyDate.ToOADate -
DateSerial(Year(MyDate) - 1, 12, 31).ToOADate), "000")
MsgBox(ss)
End Sub

[VB6]
Private Sub Command1_Click()
Dim MyDate As Date
MyDate = #11/16/2003#
ss = Format(MyDate - DateSerial(Year(MyDate) - 1, 12, 31), "000")
MsgBox ss
End Sub

FYI
All the two sample will return 320, NOT 321.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top