JavaScript
  Home arrow JavaScript arrow Page 3 - Useful Web Widgets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Useful Web Widgets
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-01-18

    Table of Contents:
  • Useful Web Widgets
  • Getting Down With Father Time
  • The Function in Depth
  • A Calendar Engine
  • The Interface Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Useful Web Widgets - The Function in Depth


    (Page 3 of 5 )

    The function accepts three arguments. The first argument is the format string, the second is the character to use as a separator between each value and the third is a boolean value to determine if we want to display the time.

    function getFormattedDate(format,separator,time)
    {
      //get a date object
       var curr_dt = new Date();
      //make sure the format string is lower case
       format = format.toLowerCase();
      //make an array of the format
       var tArr = format.split(",");
       //create a default if the separator is not specified
       if(separator == "")
       {
                            separator = " ";
       }
      //a default format object
       var formatObj = new Object();
       formatObj.monthd = true;
       formatObj.monthLong = true;
       formatObj.dayd = true;
       formatObj.dayLong = true;
       formatObj.yearLong = true;

    The format string can be optional, where you would simply pass an empty string and use the default format which is "dl,dl,l" but could be anything you like. if you wanted to make troubleshooting the function easier, you could certainly provide some validation for the format string using regular expressions at this point. If we aren't using the default, then we need to change default values before producing any date values.

       if(format != "")
       {
                   //handle month
                            if(tArr[0].charAt(0) == "t")
                            {
                                        formatObj.monthd = false
                            }
                            if(tArr[0].charAt(1) == "s")
                            {
                                        formatObj.monthLong = false;
                            }
                            //handle day
                            if(tArr[1].charAt(0) == "t")
                            {
                                        formatObj.dayd = false;
                            }
                            if(tArr[1].charAt(1) == "s")
                            {
                                        formatObj.dayLong = false;
                            }
                            //handle year
                            if(tArr[2].charAt(0) == "s")
                            {
                                        formatObj.yearLong = false;
                            }
                }

    Now it's time to start generating the date string. First up is the month. We add leading zeros if the format is "long" or simply use the month number as is + 1 (because the output of getMonth() is 0-11). When the month format is text we can use the number derived from getMonth as an index for an array that contains the text versions.

                //format month
                var mon = "";
                if(formatObj.monthd)
                {
                            if(formatObj.monthLong)
                            {
                                        mon = curr_dt.getMonth() + 1;
                                        if(mon < 10)
                                        {
                                                    mon = "0" + mon.toString();
                                        }
                            }
                            else
                            {
                                        mon = curr_dt.getMonth() + 1;
                            }
                }
                else
                {
                            if(formatObj.monthLong)
                            {
                            var monthsArr = new Array("January","Febuary","March","April","May","June","July",
    "August","September","October","November","December");
                            }
                            else
                            {
                                        var monthsArr = new Array("Jan.","Feb.","Mar.","Apr.","May.","Jun.",
    "Jul.","Aug.","Sept.","Oct.","Nov.","Dec.");
                            }
                            $tmp = curr_dt.getMonth();
                            mon = monthsArr[$tmp];;
                }

    Next is the day which is treated in a similar fashion. We also need to set up a text version of the day to be placed at the beginning of the date string when the day format is text.

                //format day
                var ddy = "";
                var dy = "";
                if(formatObj.dayd)
                {
                            if(formatObj.dayLong)
                            {
                                        ddy = curr_dt.getDate();
                                        if(ddy < 10)
                                        {
                                                    ddy = " 0" + ddy.toString() + " ";
                                        }
                            }
                            else
                            {
                                        ddy = " " + curr_dt.getDate()
    + " ";
                            }
                }
                else
                {
                            if(formatObj.dayLong)
                            {
                                        var dayArr = new Array
    ("Monday","Tuesday","Wednesday","Thursday",
    "Friday","Saturday","Sunday");
                                        ddy = " " + curr_dt.getDate() + " ";
                            }
                            else
                            {
                                        var dayArr = new Array("Mon","Tues","Wed","Thurs","Fri","Sat","Sun");
                                        ddy = " " + curr_dt.getDate() + " ";
                            }
                            var daytmp = curr_dt.getDate() - 1;
                            dy = dayArr[daytmp]
                }

    The next task is to get the year. This one is much simpler.

                //format year
                var yr = "";
                if(formatObj.yearLong)
                {
                   yr = curr_dt.getFullYear().toString();
                }
                else
                {
                   yr = curr_dt.getFullYear().toString().substr(2,2);
                }

    The last part of the date string to generate is the time. This takes the most processing. This section could be modified to support more display formats in the same way the date does.

                if(time)
                {
                            var tm = "";
                            var hours = curr_dt.getHours();
                            var minutes = curr_dt.getMinutes();
                            //if minutes are 1-9 we need a leading
    zero
                            if(minutes < 10)
                            {
                               minutes = "0" + minutes;
                            }
                            //new day starts unless you are a
    programmer
                            if(hours == 0)
                            {
                               tm = 12 + ":" + minutes + " AM";
                            }
                            //the noon hour is here so tack on a PM
                            else if(hours == 12)
                            {
                               tm = hours + ":" + minutes + " PM";
                            }
                            //we are past noon. subtract 12 to start
    at 1 and go up.
                            else if(hours > 12 )
                            {
                               tm = (hours - 12) + ":" + minutes + "
    PM";
                            }
                            else
                            {
                               tm = hours + ":" + minutes + " AM";
                            }
                }

    After all that it's time to put it all together.

                var final_str;
                final_str = dy + " " + mon + separator + ddy +
    separator + yr;
                if(time)
                {
                            final_str +=  " " + tm;
                }
                return final_str;

    You can then return "final_str" and display it any way you wish.

    More JavaScript Articles
    More By Chris Root


       · Thank you for reading the article. Comments are welcome.
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT