Announcement

Collapse
No announcement yet.

Days represented in Years, Months & Days

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Days represented in Years, Months & Days

    Has anyone written anything (I don't believe there is a function or otherwise in vbscript) to convert a number in days to Months, Days or Years, Months & Days depending on the situation?

    I began writing something and got it almost done, but it doesn't quite work for all situations. Not to mention, I'm sure it can be written much more efficiently.

    Thanks,

    -Tom
    -Tom

    Visit Kernhome
    Kern Theater Gallery

    (User: guest / Pass: guest)

    #2
    I have not had the need, but the approach I would use is someting like the following

    idays = 400
    startDate = DateAdd("d",-idays,now)
    Years = DateDiff("yyyy",startDate,now)
    EndOfYears = DateAdd("yyyy",Years,startDate)
    Months = DateDiff("m",EndOfYears,now)
    EndOfMonths = DateAdd("m",Months,EndOfYears)
    Days = DateDiff("d",EndOfMonths,now)
    msgbox Years & "," & Months & "," & Days

    Comment


      #3
      Michael,

      Thanks for the alternate approach, definitely more efficient than mine. I'll need to tweak it a bit but this gives me what I need. Thanks again.

      -Tom
      -Tom

      Visit Kernhome
      Kern Theater Gallery

      (User: guest / Pass: guest)

      Comment


        #4
        Well, here is what I have so far. I'm trying to get it to work for all situations and am about to give up. Can anyone set me straight here?

        Thanks,

        -Tom


        Function FormatDays(idays)
        Dim startdate, years, endofyears, months, endofmonths, days, stemp
        Dim Separator

        startDate = DateAdd("d", -idays, now)
        ' MsgBox "Start-" & startdate
        Years = DateDiff("yyyy", startDate, now)
        ' MsgBox "years-" & years
        If years = 0 Then
        EndOfYears = DateAdd("yyyy", years, startDate)
        Else
        EndOfYears = DateAdd("yyyy", years - 1, startDate)
        years = years - 1
        End If
        ' MsgBox "EndYears-" & endofyears
        Months = DateDiff("m", EndOfYears, now)
        ' MsgBox "months-" & months
        If months = 0 Then
        EndOfMonths = DateAdd("m", months, EndOfYears)
        Else
        EndOfMonths = DateAdd("m", months - 1, EndOfYears)
        months = months - 1
        End If
        ' MsgBox "endofmonths-" & endofmonths
        Days = DateDiff("d", EndOfMonths, now)
        ' MsgBox "days-" & days

        sTemp = ""
        If CInt(Years) = 1 Then
        sTemp = Years & " Year"
        Separator = ", "
        ElseIf CInt(Years) > 0 Then
        sTemp = Years & " Years"
        Separator = ", "
        End If

        If CInt(Months) = 1 Then
        sTemp = sTemp & Separator & Months & " Month"
        Separator = ", "
        ElseIf CInt(Months) > 0 Then
        sTemp = sTemp & Separator & Months & " Months"
        Separator = ", "
        End If

        If CInt(Days) = 1 Then
        sTemp = sTemp & Separator & Days & " Day"
        Elseif CInt(Days) > 0 Then
        sTemp = sTemp & Separator & Days & " Days"
        End If

        FormatDays = sTemp

        End Function
        -Tom

        Visit Kernhome
        Kern Theater Gallery

        (User: guest / Pass: guest)

        Comment


          #5
          Is this what you're looking for?

          <pre class="ip-ubbcode-code-pre">

          Function FormatDays(iDays)

          Dim StartDate, Years, EndOfYears, Months, EndOfMonths, Days, sTemp
          Dim CurrentDate, Separator

          CurrentDate = Now

          StartDate = DateAdd("d", -iDays, CurrentDate)

          Years = DateDiff("yyyy", StartDate, CurrentDate)
          EndOfYears = DateAdd("yyyy", Years, StartDate)

          If EndOfYears &gt; CurrentDate Then
          Years = Years - 1
          EndOfYears = DateAdd("yyyy", Years, StartDate)
          End If

          Months = DateDiff("m", EndOfYears, CurrentDate)
          EndOfMonths = DateAdd("m", Months, EndOfYears)

          If EndOfMonths &gt; CurrentDate Then
          Months = Months - 1
          EndOfMonths = DateAdd("m", Months, EndOfYears)
          End If

          Days = DateDiff("d", EndOfMonths, CurrentDate)

          sTemp = ""

          If CInt(Years) = 1 Then
          sTemp = Years & " Year"
          Separator = ", "
          ElseIf CInt(Years) &gt; 0 Then
          sTemp = Years & " Years"
          Separator = ", "
          End If

          If CInt(Months) = 1 Then
          sTemp = sTemp & Separator & Months & " Month"
          Separator = ", "
          ElseIf CInt(Months) &gt; 0 Then
          If sTemp &lt;&gt; "" Then
          sTemp = sTemp & Separator
          End If
          sTemp = sTemp & Months & " Months"
          Separator = ", "
          End If

          If CInt(Days) = 1 Then
          sTemp = sTemp & Separator & Days & " Day"
          ElseIf CInt(Days) &gt; 0 Then
          If sTemp &lt;&gt; "" Then
          sTemp = sTemp & Separator
          End If
          sTemp = sTemp & Days & " Days"
          End If

          FormatDays = sTemp

          End Function

          </pre>

          Comment


            #6
            Dave,

            I finally had time to add and test this. Thank you very much.

            -Tom
            -Tom

            Visit Kernhome
            Kern Theater Gallery

            (User: guest / Pass: guest)

            Comment

            Working...
            X