Results 1 to 3 of 3

Thread: [RESOLVED] [VB 2003] DateTime cast type

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Resolved [RESOLVED] [VB 2003] DateTime cast type

    Hi,
    I am having a problem turning nothing into a datetime. Here is the line I am using:

    VB Code:
    1. Dim DtApproved As DateTime = IIf(Me.lblRtngApprovedDate.Text = "", DateTime.MinValue, CType(Me.lblRtngApprovedDate.Text, DateTime))

    The date is stored as the text of a label (that's just the way it is, I didn't design this, I just gotta work with it). So, if the text = "", I want DtApproved to equal DateTime.MinValue, else I want it to convert the text in the label to a date.

    The second part is working, but it is crashing when the label's text equals "". Any ideas on how to fix this?

    Thanks to all

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [VB 2003] DateTime cast type

    just so you know, DateTime.MinValue is equal to January 1, 0001

    Not sure if that's what you want or not.

    Anyway, I never recommend using inline iff statements.. It is generic, so it returns object types, so they need to be casted correctly.. Basically I think its more trouble than the line of code it saves...

    anyway.. this should work

    VB Code:
    1. Try
    2.             Dim DtApproved As DateTime = DateTime.Parse(IIf(Me.lblRtngApprovedDate.Text = "", DateTime.MinValue.ToString, Me.lblRtngApprovedDate.Text).ToString)
    3.         Catch ex As Exception
    4.             MessageBox.Show("Invalid Entry")
    5.         End Try

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [VB 2003] DateTime cast type

    Thanks a lot, thats works perfectly. And thanks for the advice on not using IIf.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width