Connecting Tech Pros Worldwide Forums | Help | Site Map

Date Subtraction/Formatting Issues

David Doing
Guest
 
Posts: n/a
#1: Nov 20 '05
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



Armin Zingler
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Date Subtraction/Formatting Issues


"David Doing" <doing@cny-golf.com> schrieb[color=blue]
> 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??[/color]

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


Maybe you like the shorter version: ;-)

MyDate.DayOfYear.ToString("000")


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

David Doing
Guest
 
Posts: n/a
#3: Nov 20 '05

re: Date Subtraction/Formatting Issues


See, its so much easier in .NET!!

Thanks Armin

"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:%23wT023VrDHA.2500@TK2MSFTNGP10.phx.gbl...[color=blue]
> "David Doing" <doing@cny-golf.com> schrieb[color=green]
> > 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??[/color]
>
> (MyDate.Subtract(New Date(MyDate.Year, 12, 31))).ToString("000")
>
>
> Maybe you like the shorter version: ;-)
>
> MyDate.DayOfYear.ToString("000")
>
>
> --
> Armin
>
> http://www.plig.net/nnq/nquote.html
> http://www.netmeister.org/news/learn2quote.html
>[/color]


Peter Huang
Guest
 
Posts: n/a
#4: Nov 20 '05

re: Date Subtraction/Formatting Issues


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.

David Doing
Guest
 
Posts: n/a
#5: Nov 20 '05

re: Date Subtraction/Formatting Issues


Worked like a charm, thank you.
David
"Peter Huang" <v-phuang@online.microsoft.com> wrote in message
news:sA5eBvXrDHA.2164@cpmsftngxa06.phx.gbl...[color=blue]
> 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.
>[/color]


Closed Thread