5 Replies Latest reply on Feb 14, 2008 11:37 AM by SteveH59

    11st February?

    SteveH59 Level 1
      Hello

      I use the following Action Script to obtain the current date for my movie, but find that it shows the date as 11st February, and not 11th February:

      function getMonthAsString(month:Number):String {
      var monthNames_array:Array = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
      return monthNames_array[month];
      }

      function getDateAsString(day:Number):String {
      var retval=day.toString();
      var lastchar=retval.substr(-1);
      switch (lastchar) {
      case "1":retval+="st";
      break;
      case "2":retval+="nd";
      break;
      case "3":retval+="rd";
      break;
      default:
      retval+="th"
      break;

      }
      return retval;
      }

      //get the date now
      var now:Date = new Date();

      //populate the textfield with a formatted date
      dateField.text=getDateAsString(now.getDate())+" "+getMonthAsString(now.getMonth())+" "+now.getFullYear();

      Do I need to add to the 'case' statements to correct the problem I have?

      Many thanks for any advice.

      Steve
        • 1. Re: 11st February?
          kglad Adobe Community Professional & MVP
          you sure need to correct that switch statement, if you're going to continue to use one.
          • 2. Re: 11st February?
            Don McCahill
            The code is doing exactly what you are asking it to. If the last digit in the date is 1, it adds st. So 11st is correct.

            I would make the switch look at the entire date, and have cases for 1, 2, 3, 11, 12, 13, 21, 22, 23, and 31.
            • 3. Re: 11st February?
              Level 7
              You can't just check the last number, you're going to have the same problem
              on the 12th, and 13th - you could just add a test before the switch: if
              (day > 3 && day < 21) { retVal += "th"; }else{ //do your current code

              --
              Dave -
              Head Developer
              http://www.blurredistinction.com
              Adobe Community Expert
              http://www.adobe.com/communities/experts/


              • 4. Re: 11st February?
                Tony Heagren Level 1
                Old Skool Method: (works)


                mydate = new date();
                time = (mydate.getHours()+":"+myDate.getMinutes());
                mymonth = new date();
                month = (mymonth.getMonth());
                if (month == 1) {
                month = "February";
                } else if (month == 2) {
                month = "March";
                } else if (month == 3) {
                month = "April";
                } else if (month == 4) {
                month = "May";
                } else if (month == 5) {
                month = "June";
                } else if (month == 6) {
                month = "July";
                } else if (month == 7) {
                month = "August";
                } else if (month == 8) {
                month = "September";
                } else if (month == 9) {
                month = "October";
                } else if (month == 10) {
                month = "November";
                } else if (month == 11) {
                month = "December";
                } else if (month == 12) {
                month = "January";
                }
                myyear = new date();
                year = myyear.getFullYear();
                mynumber = new date();
                number = mynumber.getDate();
                if (number == 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20) {
                suffix = "th";
                } else if (number == 1 or 21 or 31) {
                suffix = "st";
                } else if (number == 3 or 23) {
                suffix = "rd";
                } else if (number == 2 or 22) {
                suffix = "nd";
                }

                • 5. Re: 11st February?
                  SteveH59 Level 1
                  Hello

                  Thank you all for replying. I have saved the messages and if my own does not work, I will try Tony's. I have never coded anything in Action Script before, but I tried this and I am not getting any AS errors:

                  switch (lastchar) {
                  case "1,21,31":retval+="st";
                  break;
                  case "2,22":retval+="nd";
                  break;
                  case "3,23":retval+="rd";
                  break;
                  default:
                  retval+="th"
                  break;

                  }
                  return retval;
                  }

                  This gives me 12th February (before I altered the script it showed 12nd February. Am I correct in suggesting that unless the ordinal number ends in 1,21,31,2,22,3,or 23, then it will show 'th'?

                  Cheers again

                  Steve