none
Convert Day into number RRS feed

  • Question

  • Hi there,

    I would like to convert  a date into a number. For example the 1.1.2009 should be "1", the 2.1.2009 should be "2" and so on. I plan to give out a saying of the day. I have a sayings list with 365 lines and want to pick on for every day.

    Thanks
    Saturday, May 9, 2009 8:40 AM

Answers

  • I don't think there is a single function to do this, but you can work it out in a Sub.

    GetDay()
    TextWindow.WriteLine("Days from the start of the year is "+day)
    
    Sub GetDay
      'Set the number of days in each month
      For i = 1 To 12
        If (i = 2) Then
          If (Math.Remainder(Clock.Year,4) = 0) Then
            Array.SetValue("DaysInMonth",i,29) 'Leap Year
          Else
            Array.SetValue("DaysInMonth",i,28)
          EndIf
        ElseIf (i = 9 Or i = 4 Or i = 6 Or i = 11) Then
          Array.SetValue("DaysInMonth",i,30)
        Else
          Array.SetValue("DaysInMonth",i,31)
        EndIf
      EndFor
    
      'Find the number of days before the start of each month
      Array.SetValue("DaysToStartOfMonth",1,0)
      For i = 2 To 12
        Array.SetValue("DaysToStartOfMonth",i,Array.GetValue("DaysToStartOfMonth",i-1) + Array.GetValue("DaysInMonth",i-1))
      EndFor
      
      'Set day of the year
      day = Array.GetValue("DaysToStartOfMonth",Clock.Month)+Clock.Day
    EndSub
    
    • Marked as answer by Supermethan Saturday, May 9, 2009 9:13 PM
    Saturday, May 9, 2009 9:27 AM
    Moderator

All replies

  • I don't think there is a single function to do this, but you can work it out in a Sub.

    GetDay()
    TextWindow.WriteLine("Days from the start of the year is "+day)
    
    Sub GetDay
      'Set the number of days in each month
      For i = 1 To 12
        If (i = 2) Then
          If (Math.Remainder(Clock.Year,4) = 0) Then
            Array.SetValue("DaysInMonth",i,29) 'Leap Year
          Else
            Array.SetValue("DaysInMonth",i,28)
          EndIf
        ElseIf (i = 9 Or i = 4 Or i = 6 Or i = 11) Then
          Array.SetValue("DaysInMonth",i,30)
        Else
          Array.SetValue("DaysInMonth",i,31)
        EndIf
      EndFor
    
      'Find the number of days before the start of each month
      Array.SetValue("DaysToStartOfMonth",1,0)
      For i = 2 To 12
        Array.SetValue("DaysToStartOfMonth",i,Array.GetValue("DaysToStartOfMonth",i-1) + Array.GetValue("DaysInMonth",i-1))
      EndFor
      
      'Set day of the year
      day = Array.GetValue("DaysToStartOfMonth",Clock.Month)+Clock.Day
    EndSub
    
    • Marked as answer by Supermethan Saturday, May 9, 2009 9:13 PM
    Saturday, May 9, 2009 9:27 AM
    Moderator
  • Here's another

    'list of all the days in each month, april = 30, may = 31 june = 30, etc.
    'note: feb. always has at least 28 days
    DaysInMonth = "312831303130313130313031"

    DayOfTheYear = 0
    'check for leap year
    If (Math.Remainder(Clock.Year, 4) = 0) Then
      DayOfTheYear = DayOfTheYear + 1
    EndIf
    'using daysinmonth add together previous months
    For M = 1 To Clock.Month - 1
      DayOfTheYear = DayOfTheYear + Text.GetSubText(DaysInMonth, M * 2 - 1, 2)
    EndFor
    DayOfTheYear = DayOfTheYear + Clock.Day

    TextWindow.WriteLine(DayOfTheYear)

    Saturday, May 9, 2009 4:02 PM
  • Thanks litdev, works out fine :)
    Saturday, May 9, 2009 9:13 PM