Loading ...
Sorry, an error occurred while loading the content.

Re: [JavaScript] formatdate function

Expand Messages
  • David Smart
    Your function is called formatdate but you invoke it as formatDate in the snippet of code in the top description body. Once the name of the function is
    Message 1 of 1 , Oct 1, 2004
    • 0 Attachment
      Your function is called formatdate but you invoke it as formatDate in the snippet of code in the top description body. Once the name of the function is changed to formatDate it produces an output.

      On IE 6 I get "Friday, October, 1, 3904", with FireFox I get "Friday, October, 1, 2004", and with Mozilla, and with Netscape 4.7.

      Two ways of getting round this. You can check the year you get and only add 1900 if it needs it, or you can use getFullYear instead of getYear and not add 1900 at all.

      I don't know who wrote this code, but it's certainly got a lot of wasted space in it.

      IF's don't need all those brackets - because they don't have multiple statements in them, and "ELSE IF" can save a lot of indenting and make the code easier to read.

      My version (note that I've also put spaces in front of parentheses, because I prefer them that way, and simply global change as I copy into Arachnophilia):

      /*----------------------------------------------------------------------------------*\
      | Name: formatDate
      |
      | Description: Formats a date using a format string.
      |
      | d or dd represents the day of the month (eg 1 or 01)
      | m or mm represents the month as a number (eg 1 or 01)
      | mmm or mmmm represents the month as a string (eg Jan or January)
      | y or yy represents the year (eg 99 or 1999)
      | w or ww represents the day of the week (eg Mon or Monday)
      |
      | example: formatDate (myDate, "mm/dd/yy") might look like 01/01/2000
      |
      | Parameters: strFullDate - required - the date to display (a date object)
      | strFormatString - required - a format string
      |
      | var myDate = new Date ();
      | alert (formatDate (myDate, "ww, mmmm, d, yy"));
      |
      \*-----------------------------------------------------------------------------------*/

      function formatDate (strFullDate, strFormatString)
      {
      var strMonths = Array ("January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December");
      var strDay = Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

      var strValue_d = strFullDate.getDate ();
      var strValue_dd = (strValue_d < 10) ? "0" : "" + strValue_d;
      var strValue_m = strFullDate.getMonth () + 1;
      var strValue_mm = (strValue_m < 10) ? "0" : "" + strValue_m;
      var strValue_mmmm = strMonths[strFullDate.getMonth ()];
      var strValue_mmm = strValue_mmmm.substr (0, 3);
      var strValue_yy = strFullDate.getFullYear () + "";
      var strValue_y = strValue_yy.substr (2, 2);
      var strValue_ww = strDay[strFullDate.getDay ()];
      var strValue_w = strValue_ww.substr (0, 3);

      if (strFormatString.indexOf ("dd") != -1)
      strFormatString = strFormatString.replace ("dd", "strValue_dd");
      else if (strFormatString.indexOf ("d") != -1)
      strFormatString = strFormatString.replace ("d", "strValue_d");

      if (strFormatString.indexOf ("mmmm") != -1)
      strFormatString = strFormatString.replace ("mmmm", "strValue_mmmm");
      else if (strFormatString.indexOf ("mmm") != -1)
      strFormatString = strFormatString.replace ("mmm", "strValue_mmm");
      else if (strFormatString.indexOf ("mm") != -1)
      strFormatString = strFormatString.replace ("mm", "strValue_mm");
      else if (strFormatString.indexOf ("m") != -1)
      strFormatString = strFormatString.replace ("m", "strValue_m");

      if (strFormatString.indexOf ("yy") != -1)
      strFormatString = strFormatString.replace ("yy", "strValue_yy");
      else if (strFormatString.indexOf ("y") != -1)
      strFormatString = strFormatString.replace ("y", "strValue_y");

      if (strFormatString.indexOf ("ww") != -1)
      strFormatString = strFormatString.replace ("ww", "strValue_ww");
      else if (strFormatString.indexOf ("w") != -1)
      strFormatString = strFormatString.replace ("w", "strValue_w");

      strFormatString = strFormatString.replace ("strValue_dd", strValue_dd);
      strFormatString = strFormatString.replace ("strValue_d", strValue_d);
      strFormatString = strFormatString.replace ("strValue_mmmm", strValue_mmmm);
      strFormatString = strFormatString.replace ("strValue_mmm", strValue_mmm);
      strFormatString = strFormatString.replace ("strValue_mm", strValue_mm);
      strFormatString = strFormatString.replace ("strValue_m", strValue_m);
      strFormatString = strFormatString.replace ("strValue_yy", strValue_yy);
      strFormatString = strFormatString.replace ("strValue_y", strValue_y);
      strFormatString = strFormatString.replace ("strValue_ww", strValue_ww);
      strFormatString = strFormatString.replace ("strValue_w", strValue_w);

      return strFormatString;
      }

      var myDate = new Date ();
      document.write (formatDate (myDate, "ww, mmmm, d, yy"));

      =================================

      Note that I also changed the indexOf checks to "!= -1", rather than "> -1". The -1 is a flag, not a position, so the code reads better if you check specifically for it. Alternately, you could look for ">= 0" because 0 is a position. Just a matter of style, but I reckon it makes it easier to read later.

      And, of course, the last series of replaces could all be done in one statement, with the .replace clauses simply tacked on one after the other in the return:

      return strFormatString.replace ("strValue_dd", strValue_dd)
      .replace ("strValue_d", strValue_d)
      .replace ("strValue_mmmm", strValue_mmmm)
      .replace ("strValue_mmm", strValue_mmm)
      .replace ("strValue_mm", strValue_mm)
      .replace ("strValue_m", strValue_m)
      .replace ("strValue_yy", strValue_yy)
      .replace ("strValue_y", strValue_y)
      .replace ("strValue_ww", strValue_ww)
      .replace ("strValue_w", strValue_w);

      IMHO it is again easier to read. It also has the added benefit of probably executing faster in the browser.

      Dave S

      ----- Original Message -----
      From: Marcello Miorelli
      To: Javascript Group
      Sent: Friday, October 01, 2004 12:02 AM
      Subject: [JavaScript] formatdate function



      Hi all,
      Thanks for all useful messages, specially Dave.

      I have got this function to formatdate but I could not
      use it. It says Object Expected. How could I do that?


      /*----------------------------------------------------------------------------------*\
      | Name: formatdate
      |
      | Description: Formats a date using a format string.
      |
      | d or dd represents the day of the month
      (eg 1 or 01)
      | m or mm represents the month as a
      number (eg 1 or 01)
      | mmm or mmmm represents the month as a
      string (eg Jan or January)
      | y or yy represents the year (eg 99 or
      1999)
      | w or ww represents the day of the week
      (eg Mon or Monday)
      |
      | example: formatDate(myDate,
      "mm/dd/yy") might look like 01/01/2000
      |
      | Parameters: strFullDate - required - the date to
      display (a date object)
      | strFormatString - required - a format
      string
      |
      | var myDate = new Date();
      | alert(formatDate(myDate, "ww, mmmm, d, yy"));
      |
      \*-----------------------------------------------------------------------------------*/


      function formatdate(strFullDate, strFormatString) {


      var strMonths = new Array();
      var strDay = new Array();

      strMonths[0] = "January";
      strMonths[1] = "February";
      strMonths[2] = "March";
      strMonths[3] = "April";
      strMonths[4] = "May";
      strMonths[5] = "June";
      strMonths[6] = "July";
      strMonths[7] = "August";
      strMonths[8] = "September";
      strMonths[9] = "October";
      strMonths[10] = "November";
      strMonths[11] = "December";

      strDay[0] = "Sunday";
      strDay[1] = "Monday";
      strDay[2] = "Tuesday";
      strDay[3] = "Wednesday";
      strDay[4] = "Thursday";
      strDay[5] = "Friday";
      strDay[6] = "Saturday";

      var strValue_d = strFullDate.getDate();
      var strValue_dd = (strValue_d < 10) ? '0' +
      strValue_d : strValue_d;
      var strValue_m = strFullDate.getMonth() + 1;
      var strValue_mm = (strValue_m < 10) ? '0' +
      strValue_m : strValue_m;
      var strValue_mmmm =
      strMonths[strFullDate.getMonth()];
      var strValue_mmm = strValue_mmmm.substr(0,3);
      var strValue_yy = strFullDate.getYear() + 1900 +
      "";
      var strValue_y = strValue_yy.substr(2,2);
      var strValue_ww = strDay[strFullDate.getDay()];
      var strValue_w = strValue_ww.substr(0,3);

      if (strFormatString.indexOf("dd") > -1) {
      strFormatString = strFormatString.replace("dd",
      "strValue_dd");
      }
      else {
      if (strFormatString.indexOf("d") > -1) {
      strFormatString = strFormatString.replace("d",
      "strValue_d");
      }
      }

      if (strFormatString.indexOf("mmmm") > -1) {
      strFormatString = strFormatString.replace("mmmm",
      "strValue_mmmm");
      }
      else {
      if (strFormatString.indexOf("mmm") > -1) {
      strFormatString = strFormatString.replace("mmm",
      "strValue_mmm");
      }
      else {
      if (strFormatString.indexOf("mm") > -1) {
      strFormatString = strFormatString.replace("mm",
      "strValue_mm");
      }
      else {
      if (strFormatString.indexOf("m") > -1) {
      strFormatString = strFormatString.replace("m",
      "strValue_m");
      }
      }
      }
      }

      if (strFormatString.indexOf("yy") > -1) {
      strFormatString = strFormatString.replace("yy",
      "strValue_yy");
      }
      else {
      if (strFormatString.indexOf("y") > -1) {
      strFormatString = strFormatString.replace("y",
      "strValue_y");
      }
      }

      if (strFormatString.indexOf("ww") > -1) {
      strFormatString = strFormatString.replace("ww",
      "strValue_ww");
      }
      else {
      if (strFormatString.indexOf("w") > -1) {
      strFormatString = strFormatString.replace("w",
      "strValue_w");
      }
      }

      strFormatString =
      strFormatString.replace("strValue_dd", strValue_dd);
      strFormatString =
      strFormatString.replace("strValue_d", strValue_d);
      strFormatString =
      strFormatString.replace("strValue_mmmm",
      strValue_mmmm);
      strFormatString =
      strFormatString.replace("strValue_mmm", strValue_mmm);
      strFormatString =
      strFormatString.replace("strValue_mm", strValue_mm);
      strFormatString =
      strFormatString.replace("strValue_m", strValue_m);
      strFormatString =
      strFormatString.replace("strValue_yy", strValue_yy);
      strFormatString =
      strFormatString.replace("strValue_y", strValue_y);
      strFormatString =
      strFormatString.replace("strValue_ww", strValue_ww);
      strFormatString =
      strFormatString.replace("strValue_w", strValue_w);

      return strFormatString;
      }


      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/04

      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.