Results 1 to 6 of 6

Thread: [RESOLVED]Need help coding from date to num and back again

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Location
    Lake Charles, LA
    Posts
    16

    [RESOLVED]Need help coding from date to num and back again

    Its late and this is killing me nothing major or detrimental just working on some code and its kicking my rump. Any help is appreciated.

    Here is the goal. to take a string in the format "##/##/####" and convert it into an integer. 1/1/1 being equal to 1 and 1/2/1 equal to 2 and 12/31/1 equal to 365. leap year is accounted for every year that is divisable by 4 but not divisible by 100 unless it is also divisible by 400.

    ok that sai d i want to be able to reverse that code and convert the number back into a date. So i should be able to add 5 to the number and get a date 5 days from now. i am almost there however if you put in 12/31/2005 and add 1 you get 12/32/2005 I have looked over it and cant find my error.

    Thanks in advance if you find my mistake

    the functions are datetonum(byval str1 as string) and numtodate(byval num1 as integer)

    here is the code

    VB Code:
    1. Function datetonum(ByVal str1 As String)
    2.  
    3.         Dim length As Integer
    4.         Dim noofdays As String
    5.         Dim char1 As Char
    6.         Dim marker As Integer = 1
    7.         Dim month As String = ""
    8.         Dim day As String = ""
    9.         Dim year As String = ""
    10.         Dim leap As Boolean = False
    11.  
    12.  
    13.  
    14.  
    15.  
    16.         length = Len(str1)
    17.  
    18.         For i As Integer = 1 To length
    19.             char1 = Mid(str1, i, 1)
    20.             If char1 = "/" Then
    21.                 marker = marker + 1
    22.             Else
    23.                 If marker = 1 Then
    24.                     month = month & char1
    25.                 End If
    26.                 If marker = 2 Then
    27.                     day = day & char1
    28.                 End If
    29.                 If marker = 3 Then
    30.                     year = year & char1
    31.                 End If
    32.             End If
    33.         Next
    34.         If month <= 9 Then
    35.             month = "0" & month
    36.         End If
    37.         If day <= 9 Then
    38.             day = "0" & day
    39.         End If
    40.         year = Mid(year, 1, 4)
    41.  
    42.  
    43.  
    44.  
    45.         noofdays = day + monthtodays(month, year) + yearstodays(year)
    46.  
    47.         Return noofdays
    48.  
    49.  
    50.     End Function
    51.  
    52.     Function yearstodays(ByVal num1 As Integer)
    53.         Dim years As Integer
    54.         Dim leap As Boolean = False
    55.         Dim days As Integer = 0
    56.  
    57.  
    58.  
    59.         Do While years < num1 - 1
    60.             If years Mod 4 = 0 And years > 3 Then
    61.                 If Not years Mod 100 = 0 Then
    62.                     leap = True
    63.                 Else
    64.                     If years Mod 400 = 0 Then
    65.                         leap = True
    66.                     End If
    67.                 End If
    68.             End If
    69.  
    70.             If leap Then
    71.                 days = days + 366
    72.             Else
    73.                 days = days + 365
    74.             End If
    75.             years = years + 1
    76.         Loop
    77.  
    78.  
    79.  
    80.  
    81.         Return days
    82.  
    83.     End Function
    84.  
    85.     Function monthtodays(ByVal num1 As Integer, ByVal year As Integer)
    86.         Dim month As Integer
    87.         Dim leap As Boolean = False
    88.         Dim days As Integer = 0
    89.  
    90.         For month = 0 To num1 - 1
    91.             If year Mod 4 = 0 And year > 3 Then
    92.                 If Not year Mod 100 = 0 Then
    93.                     leap = True
    94.                 Else
    95.                     If year Mod 400 = 0 Then
    96.                         leap = True
    97.                     End If
    98.                 End If
    99.             End If
    100.  
    101.             If month = 4 Or month = 6 Or month = 9 Or month = 11 Then
    102.                 days = days + 30
    103.             End If
    104.             If month = 2 Then
    105.                 If leap Then
    106.                     days = days + 29
    107.                 Else
    108.                     days = days + 28
    109.                 End If
    110.             End If
    111.             If month = 1 Or month = 3 Or month = 5 Or month = 7 Or month = 8 Or month = 10 Or month = 12 Then
    112.                 days = days + 31
    113.             End If
    114.         Next month
    115.  
    116.         Return days
    117.  
    118.     End Function
    119.  
    120.  
    121.     Function numtodate(ByVal int1 As Integer)
    122.  
    123.         Dim year As String
    124.         Dim month As Integer = 1
    125.         Dim days As Integer = 1
    126.         Dim test As Integer = int1
    127.         Dim leap As Boolean = False
    128.         Dim returndate As String
    129.  
    130.  
    131.         year = findyear(int1)
    132.         month = findmonth(int1 - datetonum("1/1/" & year))
    133.         days = int1 - (datetonum(month & "/1/" & year)) + 1
    134.  
    135.         If month <= 9 Then
    136.             month = "0" & month
    137.         End If
    138.         If days <= 9 Then
    139.             days = "0" & days
    140.         End If
    141.         year = Mid(year, 1, 4)
    142.  
    143.         returndate = month & "/" & days & "/" & year
    144.  
    145.         Return returndate
    146.  
    147.     End Function
    148.  
    149.     Function findyear(ByVal int1 As Integer)
    150.         Dim year As Integer = 1
    151.         Dim month As Integer = 1
    152.         Dim days As Integer = 1
    153.         Dim test As Integer = int1
    154.         Dim leap As Boolean = False
    155.  
    156.         While (test > 365 And leap = False) Or (test > 366 And leap = True)
    157.  
    158.  
    159.             If year Mod 4 = 0 And year > 3 Then
    160.                 If Not year Mod 100 = 0 Then
    161.                     leap = True
    162.                 Else
    163.                     If year Mod 400 = 0 Then
    164.                         leap = True
    165.                     End If
    166.                 End If
    167.             End If
    168.             If leap = True Then
    169.                 test = test - 366
    170.             Else
    171.                 test = test - 365
    172.             End If
    173.             year = year + 1
    174.  
    175.         End While
    176.  
    177.         Return year
    178.  
    179.     End Function
    180.  
    181.     Function findmonth(ByVal int1 As Integer)
    182.         Dim year As Integer = 1
    183.         Dim month As Integer = 1
    184.         Dim days As Integer = 1
    185.         Dim test As Integer = int1
    186.         Dim leap As Boolean = False
    187.         While test > 31
    188.             If month = 4 Or month = 6 Or month = 9 Or month = 11 Then
    189.                 test = test - 30
    190.             End If
    191.             If month = 2 Then
    192.                 If leap Then
    193.                     test = test - 29
    194.                 Else
    195.                     test = test - 28
    196.                 End If
    197.             End If
    198.             If month = 1 Or month = 3 Or month = 5 Or month = 7 Or month = 8 Or month = 10 Or month = 12 Then
    199.                 test = test - 31
    200.             End If
    201.             month = month + 1
    202.         End While
    203.  
    204.         Return month
    205.     End Function
    Last edited by Stimpson; Dec 31st, 2005 at 05:01 PM.

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