DateTime conversion to Integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Foster

    #1

    DateTime conversion to Integer

    Good afternoon all,

    I am trying to convert a date to an integer in Microsoft Visual Basic
    2003 .NET and am experiencing a little trouble trying to get it done.

    This is what I want to do:

    Date_001 = Array_001(0)
    Date_002 = Array_001(1)
    If (Date_001 - Date_002) <= Array_002(i, 0) Then
    Value_Reference = i
    Value_Found = True
    Exit For
    Else
    End If

    However, I have to convert the Date_001 and Date_002 variables to
    Decimals or Integers before I can do this

    If (System.Convert .ToDecimal(Date _001) -
    System.Convert. ToDecimal(Date_ 002)) <= Array_002(i, 0) Then

    I have tried using the Change type, but this returns a 'Run-time
    exception thrown : System.InvalidC astException - Cast from type 'Date'
    to type 'Integer' is not valid.' error.

    (Convert.Change Type(Creation_D ate, GetType(DateTim e)), Integer)

    Any assistance you may be able to provide would be greatly appreciated.

    Yours sincerely,

    William Foster

    *** Sent via Developersdex http://www.developersdex.com ***
  • Cor Ligthert [MVP]

    #2
    Re: DateTime conversion to Integer

    William,

    Can you show us with a sample date how you want to convert a DateTime to an
    integer.

    Therefore what should the date 2006-8-18 (in ISO notation) have to look.

    A DateTime is a structure that has many members.

    Cor

    "William Foster" <nospam@devdex. comschreef in bericht
    news:OCslCSowGH A.4972@TK2MSFTN GP05.phx.gbl...
    Good afternoon all,
    >
    I am trying to convert a date to an integer in Microsoft Visual Basic
    2003 .NET and am experiencing a little trouble trying to get it done.
    >
    This is what I want to do:
    >
    Date_001 = Array_001(0)
    Date_002 = Array_001(1)
    If (Date_001 - Date_002) <= Array_002(i, 0) Then
    Value_Reference = i
    Value_Found = True
    Exit For
    Else
    End If
    >
    However, I have to convert the Date_001 and Date_002 variables to
    Decimals or Integers before I can do this
    >
    If (System.Convert .ToDecimal(Date _001) -
    System.Convert. ToDecimal(Date_ 002)) <= Array_002(i, 0) Then
    >
    I have tried using the Change type, but this returns a 'Run-time
    exception thrown : System.InvalidC astException - Cast from type 'Date'
    to type 'Integer' is not valid.' error.
    >
    (Convert.Change Type(Creation_D ate, GetType(DateTim e)), Integer)
    >
    Any assistance you may be able to provide would be greatly appreciated.
    >
    Yours sincerely,
    >
    William Foster
    >
    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • R. MacDonald

      #3
      Re: DateTime conversion to Integer

      Hello, William,

      What type is Array_002? The Date type has a Subtract method that
      returns a TimeSpan. Perhaps it would be simpler for you to convert
      Array_002 to a TimeSpan.

      Cheers
      Randy


      William Foster wrote:
      Good afternoon all,
      >
      I am trying to convert a date to an integer in Microsoft Visual Basic
      2003 .NET and am experiencing a little trouble trying to get it done.
      >
      This is what I want to do:
      >
      Date_001 = Array_001(0)
      Date_002 = Array_001(1)
      If (Date_001 - Date_002) <= Array_002(i, 0) Then
      Value_Reference = i
      Value_Found = True
      Exit For
      Else
      End If
      >
      However, I have to convert the Date_001 and Date_002 variables to
      Decimals or Integers before I can do this
      >
      If (System.Convert .ToDecimal(Date _001) -
      System.Convert. ToDecimal(Date_ 002)) <= Array_002(i, 0) Then
      >
      I have tried using the Change type, but this returns a 'Run-time
      exception thrown : System.InvalidC astException - Cast from type 'Date'
      to type 'Integer' is not valid.' error.
      >
      (Convert.Change Type(Creation_D ate, GetType(DateTim e)), Integer)
      >
      Any assistance you may be able to provide would be greatly appreciated.
      >
      Yours sincerely,
      >
      William Foster
      >
      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • William Foster

        #4
        Re: DateTime conversion to Integer

        Cor,

        The data is formatted to the en-AU Region, i.e. dd/mm/yyyy; as different
        to the American format of mm/dd/yyyy.

        I have set the data format via the System.Convert. ToDateTime formula
        using the afore mentioned region.

        No matter what I do I get the InvalidCast error, I have tried quite a
        few permutations of the Convert method without success.

        Yours sincerely,

        William Foster

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • William Foster

          #5
          Re: DateTime conversion to Integer

          Randy,

          Array_002 does not have a defined DateType as there are many different
          DataTypes within the Array, i.e. Strings, Decimals, DateTimes, Doubles
          (I think that is it). As such I can not define it as a straight
          DataType.

          I need the formula to return a number of days, as different variables
          have actions based on the number of days between the dates.

          Thanks for your assistance !

          Thanks go to Cor as well (I think I forgot to write thank you in my
          reply to his/her response).

          Yours sincerely,

          William Foster

          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: DateTime conversion to Integer

            William,

            A DateTime has to keep the real date and time in its structure no known
            format. It are ticks in 100th of a nanoseconds as longs starting 1-1-00.

            To get a day of the year from that by instance you can say

            dim theDayOfThisYea r as integer = now.dayof year

            (By the way in oposite of C# shows the Visual Basic debugger only dates in
            USA notation)

            I hope this helps,

            Cor

            "William Foster" <nospam@devdex. comschreef in bericht
            news:uaiV89pwGH A.4972@TK2MSFTN GP05.phx.gbl...
            Cor,
            >
            The data is formatted to the en-AU Region, i.e. dd/mm/yyyy; as different
            to the American format of mm/dd/yyyy.
            >
            I have set the data format via the System.Convert. ToDateTime formula
            using the afore mentioned region.
            >
            No matter what I do I get the InvalidCast error, I have tried quite a
            few permutations of the Convert method without success.
            >
            Yours sincerely,
            >
            William Foster
            >
            *** Sent via Developersdex http://www.developersdex.com ***

            Comment

            • William Foster

              #7
              Re: DateTime conversion to Integer

              Cor,

              Thanks for that, I will hopefully just be able to use a different Data
              Type to the DateTime one and everything should work well.

              Thank you for your assistance !

              Yours sincerely,

              William Foster

              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              • Phill W.

                #8
                Re: DateTime conversion to Integer

                William Foster wrote:
                I am trying to convert a date to an integer in Microsoft Visual Basic
                2003 .NET and am experiencing a little trouble trying to get it done.
                I'm not surprised. Unlike VB6 and Excel, I don't think there is a
                simple conversion from one to the other.
                This is what I want to do:
                >
                If (Date_001 - Date_002) <= Array_002(i, 0) Then
                However, I have to convert the Date_001 and Date_002 variables to
                Decimals or Integers before I can do this
                No you don't. The DateTime class has a Subtract method that works out
                the difference between one dat and another and returns you a TimeSpan
                representing that interval. From this, you can extract the number of
                days (or whatever), so

                Dim iDiff as Integer
                = Date_001.Subtra ct( Date_002 ).TotalDays

                If iDiff <= Array_002(i, 0 ) Then

                should do that nicely.

                HTH,
                Phill W.

                Comment

                • William Foster

                  #9
                  Re: DateTime conversion to Integer

                  Phil,

                  Yes, you are correct !

                  After the feedback from Cor and the other contributor I ended up using
                  the inbuilt DateDiff method.

                  Days_Difference = DateDiff(XXX.Da y,Date_001,Date _002)

                  I did find a method to do the convert within Visual Studio, only I can't
                  remember the name of the function. Basically you needed to set up a
                  conversion class (I am on a different machine at the moment so I don't
                  have access to the reference). However, the DateDiff function is much
                  much easier than going through all that.

                  Thank you to everyone for your assistance.

                  Yours sincerely,

                  William Foster

                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  Working...