Results 1 to 6 of 6

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

  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.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Need help coding from date to num and back again

    The DateTime class has a handy .DayOfYear property, and you can parse your string right into it Try the code below:
    VB Code:
    1. Dim MyDate As String = "12/31/2005"
    2.         Dim DateNow As DateTime
    3.         DateNow = Date.Parse(MyDate)
    4.         MsgBox(DateNow.DayOfYear.ToString) 'displays "365"
    A lot less code It takes leap years into account as well, you don't have to worry about it. There are also properties to display just the month, etc.. lots of things you can do with the class...

    ***EDIT - there is also an .AddDays method that can add the number of days you want, as stated in your above post.. the code below adds one day to the above code..
    VB Code:
    1. Dim MyDate As String = "12/31/2005"
    2.         Dim DateNow As DateTime
    3.         DateNow = Date.Parse(MyDate)
    4.         DateNow = DateNow.AddDays(1)
    5.         MsgBox(DateNow.DayOfYear.ToString) 'displays "1", since it will be 01/01/2006
    Last edited by gigemboy; Dec 31st, 2005 at 05:48 AM.

  3. #3

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

    Re: Need help coding from date to num and back again

    if i use that how do i go back from the number to the date? and what if the date is 2 years ago?

    thanks for the reply

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Need help coding from date to num and back again

    The date being two years ago won't matter at all, notice I just passed in a string that was in the format of a date. You can put any date you want and it will work. To go back, there are methods from the same datetime class (reading up on the class and its methods might be helpful ) You will notice that there is a .ToShortDateString that will convert it back with one line of code, so now adding it to the above code:
    VB Code:
    1. Dim MyDate As String = "12/31/2005"
    2.         Dim DateNow As DateTime
    3.         DateNow = Date.Parse(MyDate)
    4.         MsgBox(DateNow.DayOfYear.ToString) 'displays "365"
    5.         Dim DateToString As String
    6.         DateToString = DateNow.ToShortDateString 'converts it back to the original date
    7.         MsgBox(DateToString) 'displays the original "12/31/2005" string

  5. #5

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

    Re: Need help coding from date to num and back again

    thanks very much.

    however i woke up this morning in a do or die mood and came up with the solution. I am posting my code here for anyone interested just in case it might help someone else.

    Riley Belile

    VB Code:
    1. Function datetonum(ByVal str1 As String)
    2.         Dim length As Integer
    3.         Dim str2 As String
    4.         Dim char1 As Char
    5.         Dim marker As Integer = 1
    6.         Dim month As String
    7.         Dim day As String
    8.         Dim year As String
    9.         Dim num1 As Integer
    10.         Dim num2 As Integer
    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.         num1 = day + monthtodays(month, year) + yearstodays(year)
    44.  
    45.  
    46.         Return num1
    47.  
    48.  
    49.     End Function
    50.  
    51.  
    52.     Function yearstodays(ByVal num1 As Integer)
    53.         Dim years As Integer = 1
    54.         Dim leap As Boolean = False
    55.         Dim days As Integer = 0
    56.         Dim test As Integer
    57.  
    58.  
    59.  
    60.         Do While years <= num1
    61.             leap = False
    62.             test = years Mod 4
    63.             If years Mod 4 = 0 And years > 3 Then
    64.                 If Not years Mod 100 = 0 Then
    65.                     leap = True
    66.                 Else
    67.                     If years Mod 400 = 0 Then
    68.                         leap = True
    69.                     Else
    70.                         leap = False
    71.                     End If
    72.                 End If
    73.             End If
    74.  
    75.             If leap Then
    76.                 days = days + 366
    77.             Else
    78.                 days = days + 365
    79.             End If
    80.             years = years + 1
    81.         Loop
    82.  
    83.  
    84.  
    85.  
    86.         Return days - 365
    87.  
    88.  
    89.  
    90.     End Function
    91.  
    92.     Function monthtodays(ByVal num1 As Integer, ByVal year As String)
    93.  
    94.         Dim leap As Boolean
    95.  
    96.  
    97.  
    98.  
    99.         If year Mod 4 = 0 And year > 3 Then
    100.             If Not year Mod 100 = 0 Then
    101.                 leap = True
    102.             Else
    103.                 If year Mod 400 = 0 Then
    104.                     leap = True
    105.                 End If
    106.             End If
    107.         End If
    108.  
    109.         If leap = False Then
    110.  
    111.             Select Case (num1)
    112.                 Case 1 ' jan
    113.                     Return 0
    114.                 Case 2 'feb
    115.                     Return 31 ' for jan
    116.                 Case 3 'mar
    117.                     Return 28 + 31 'for jan and feb
    118.                 Case 4 'apr
    119.                     Return 31 + 28 + 31
    120.                 Case 5 'may
    121.                     Return 31 + 28 + 31 + 30
    122.                 Case 6 'june
    123.                     Return 31 + 28 + 31 + 30 + 31
    124.                 Case 7 'july
    125.                     Return 31 + 28 + 31 + 30 + 31 + 30
    126.                 Case 8 'aug
    127.                     Return 31 + 28 + 31 + 30 + 31 + 30 + 31
    128.                 Case 9 'sep
    129.                     Return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31
    130.                 Case 10 'oct
    131.                     Return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    132.                 Case 11 'nov
    133.                     Return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    134.                 Case 12 'dec
    135.                     Return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    136.             End Select
    137.  
    138.  
    139.         Else 'leap is true
    140.             Select Case (num1)
    141.                 Case 1 ' jan
    142.                     Return 0
    143.                 Case 2 'feb
    144.                     Return 31
    145.                 Case 3 'mar
    146.                     Return 31 + 29
    147.                 Case 4 'apr
    148.                     Return 31 + 29 + 31
    149.                 Case 5 'may
    150.                     Return 31 + 29 + 31 + 30
    151.                 Case 6 'june
    152.                     Return 31 + 29 + 31 + 30 + 31
    153.                 Case 7 'july
    154.                     Return 31 + 29 + 31 + 30 + 31 + 30
    155.                 Case 8 'aug
    156.                     Return 31 + 29 + 31 + 30 + 31 + 30 + 31
    157.                 Case 9 'sep
    158.                     Return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31
    159.                 Case 10 'oct
    160.                     Return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    161.                 Case 11 'nov
    162.                     Return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    163.                 Case 12 'dec
    164.                     Return 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    165.             End Select
    166.         End If
    167.  
    168.  
    169.  
    170.     End Function
    171.  
    172.  
    173.  
    174.     Function numtodate(ByVal int1 As Integer)
    175.  
    176.         Dim year As String
    177.         Dim month As Integer = 1
    178.         Dim days As Integer = 1
    179.         Dim test As Integer = int1
    180.         Dim leap As Boolean = False
    181.         Dim returndate As String
    182.  
    183.  
    184.         year = findyear(int1)
    185.         month = findmonth(int1 - yearstodays(year), year)
    186.         days = int1 - monthtodays(month, year) - yearstodays(year)
    187.  
    188.         If month <= 9 Then
    189.             month = "0" & month
    190.         End If
    191.         If days <= 9 Then
    192.             days = "0" & days
    193.         End If
    194.         year = Mid(year, 1, 4)
    195.  
    196.         returndate = month & "/" & days & "/" & year
    197.  
    198.         Return returndate
    199.  
    200.     End Function
    201.  
    202.     Function findyear(ByVal int1 As Integer)
    203.         Dim year As Integer = 1
    204.         Dim averageyear As Double = 365.2425
    205.  
    206.         year = Math.Floor((int1 + 365.0) / averageyear)
    207.  
    208.         Return year
    209.  
    210.     End Function
    211.  
    212.  
    213.     Function findmonth(ByVal int1 As Integer, ByVal years As Integer)
    214.  
    215.         Dim leap As Boolean = False
    216.         Dim count1 As Integer
    217.         Dim test As Integer
    218.         Dim month As Integer
    219.  
    220.  
    221.         If years Mod 4 = 0 And years > 3 Then
    222.             If Not years Mod 100 = 0 Then
    223.                 leap = True
    224.             Else
    225.                 If years Mod 400 = 0 Then
    226.                     leap = True
    227.                 Else
    228.                     leap = False
    229.                 End If
    230.             End If
    231.         End If
    232.  
    233.  
    234.         For count1 = 1 To 12
    235.             If leap = False Then
    236.                 Select Case (count1)
    237.                     Case 1 ' jan
    238.                         test = 0
    239.                     Case 2 'feb
    240.                         test = 31 ' for jan
    241.                     Case 3 'mar
    242.                         test = 28 + 31 'for jan and feb
    243.                     Case 4 'apr
    244.                         test = 31 + 28 + 31
    245.                     Case 5 'may
    246.                         test = 31 + 28 + 31 + 30
    247.                     Case 6 'june
    248.                         test = 31 + 28 + 31 + 30 + 31
    249.                     Case 7 'july
    250.                         test = 31 + 28 + 31 + 30 + 31 + 30
    251.                     Case 8 'aug
    252.                         test = 31 + 28 + 31 + 30 + 31 + 30 + 31
    253.                     Case 9 'sep
    254.                         test = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31
    255.                     Case 10 'oct
    256.                         test = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    257.                     Case 11 'nov
    258.                         test = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    259.                     Case 12 'dec
    260.                         test = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    261.                 End Select
    262.             Else 'leap is true
    263.                 Select Case (count1)
    264.                     Case 1 ' jan
    265.                         test = 0
    266.                     Case 2 'feb
    267.                         test = 31
    268.                     Case 3 'mar
    269.                         test = 31 + 29
    270.                     Case 4 'apr
    271.                         test = 31 + 29 + 31
    272.                     Case 5 'may
    273.                         test = 31 + 29 + 31 + 30
    274.                     Case 6 'june
    275.                         test = 31 + 29 + 31 + 30 + 31
    276.                     Case 7 'july
    277.                         test = 31 + 29 + 31 + 30 + 31 + 30
    278.                     Case 8 'aug
    279.                         test = 31 + 29 + 31 + 30 + 31 + 30 + 31
    280.                     Case 9 'sep
    281.                         test = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31
    282.                     Case 10 'oct
    283.                         test = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    284.                     Case 11 'nov
    285.                         test = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    286.                     Case 12 'dec
    287.                         test = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    288.                 End Select
    289.             End If
    290.             If int1 > test Then
    291.  
    292.                 month = count1
    293.  
    294.             End If
    295.         Next count1
    296.  
    297.         Return month
    298.     End Function


    I love this forum

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

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

    Well congrats on getting it I know its a lot more code, but sometimes when you spend enough time on something and finally get it to work, you are more inclined to use it, even if there is a much shorter way...

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