+ Reply to Thread
Results 1 to 4 of 4

Thread: JavaScript Dates - Get Methods

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    JavaScript Dates - Get Methods

    JavaScript Dates

    The JavaScript language contains a very useful Date object for manipulating dates. With this object you can do all sorts of really fun and interesting things.

    First we are going to look at how to get a Date object.

    Try out this code:

    Code:
    <script language="javascript">
    var theDate = new Date();
    </script>
    For those of you not familiar with the use of objects. The new keyword means to create a new instance of the Date object. This calls a special function known as the constructor to initialize the object. This constructor that we just used initializes a Date object with the current date.

    Now let us display the date to the screen.

    Code:
    document.write(theDate);
    When you run this code you get this result:

    Sun Oct 18 2009 05:40:16 GMT-0700 (Pacific Daylight Time)
    That is a lot of information that you may not always want. The date class provides methods for being more specific.



    The getHours method is very simple it gets the number of hours in the current date object. The getDay method returns the day of the week. The return value will be between 0 and 6 where 0 is Sunday and 6 is Saturday. Most of these methods are pretty self explanatory. One of them that might not make as much sense is the getTime method.

    This method returns the number of milliseconds that have past since January 1st 1970 at midnight. This value refers to the UNIX epoch and hence is know as the UNIX timestamp. You may ask so what? What is the point? Well those who are familiar with PHP know that this is the most flexible method of working with dates.

    If the user chooses a time, you can store the UNIX timestamp in the database and display it however you like. You can easily change how the date is displayed. You are not restricted to any format of date.

    Displaying the Current Time

    We are going to use the above methods to display the day of the week and the current time.

    Example:

    Sunday, October 18, 1992 5:57:05 AM
    The first thing we need is to get all the information we need.

    Code:
    var nMonth = theDate.getMonth();
    var nDate = theDate.getDate();
    var nDay = theDate.getDay();
    var nYear = theDate.getFullYear();
    var nHours = theDate.getHours();
    var nMinutes = theDate.getMinutes();
    var nSeconds = theDate.getSeconds();
    By using the methods in the date class we are almost done already. Besides displaying it we are done already. However, we know that nDate and nMonth hold integer values that represent a month. How are we going to convert these to strings?

    Since January is 0, I think an array is good to use here.

    Code:
    var arsMonths = new Array();
    arsMonths[0] = "January";
    arsMonths[1] = "February";
    arsMonths[2] = "March";
    arsMonths[3] = "April";
    arsMonths[4] = "May";
    arsMonths[5] = "June";
    arsMonths[6] = "July";
    arsMonths[7] = "August";
    arsMonths[8] = "September";
    arsMonths[9] = "October";
    arsMonths[10] = "November";
    arsMonths[11] = "December";
    Now notice how January is stored at index 0? This means that we get direct access to what the month actually is by using:

    Code:
    arsMonths[theDate.getMonth()];
    We need something similar for the use of days.

    Code:
    var arsDays = new Array();
    arsDays[0] = "Sunday";
    arsDays[1] = "Monday";
    arsDays[2] = "Tuesday";
    arsDays[3] = "Wednesday";
    arsDays[4] = "Thursday";
    arsDays[5] = "Friday";
    arsDays[6] = "Saturday";
    We once again get direct access to what day it is by using:

    Code:
    arsDays[theDate.getDay()];
    At this point it is simply a matter of using an output statement to display the date.

    Code:
    document.write(arsDays[nDay] + ", " + arsMonths[nMonth] + " " + nDate + ", " + nYear + " " + nHours + ":" + nMinutes + ":" + nSeconds);
    When you run this code it should show:

    Sunday, October 18, 2009 6:11:49
    Now what if we want to display if it PM or AM? In a 12 hour clock system, we use PM and AM to show if the time is before or after noon. However you should note that all times are given in 24 hour time.

    Changing to 12 hour time

    To change to 12 hour time, we simply look if the hours are greater than 12, if they are we take away 12 and set a suffix to PM. Easy.

    Code:
    if (nHours > 12) {
    	nHours -= 12;	
    	sSuffix = "P.M.";
    } else {
    	sSuffix = "A.M.";
    }
    Now we simply display sSuffix at the very end.

    Code:
    document.write(arsDays[nDay] + ", " + arsMonths[nMonth] + " " + nDate + ", " + nYear + " " + nHours + ":" + nMinutes + ":" + nSeconds + " " + sSuffix);
    Now consider if the time is 5:03:02. JavaScript is not going to display these leading zeroes for you. We have to check if we need them and add them ourselves. This is easy. We need a leading zero if minutes or seconds is less than 10.

    Code:
    if (nMinutes < 10) {
    	nMinutes = "0" + nMinutes;	
    }
    
    if (nSeconds < 10) {
    	nSeconds = "0" + nSeconds;	
    }
    That is all. I hope that you have seen that working with dates is fairly simple. You should know that there is more that you can do, but that is a topic for another day.

    You can view the results here.
    Attached Thumbnails JavaScript Dates - Get Methods-methods.jpg  
    Last edited by chili5; 10-18-2009 at 08:40 AM.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,673
    Blog Entries
    57

    Re: JavaScript Dates - Get Methods

    JavaScript date handling gets tricky with calendar info, etc. But it's worth the trouble +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: JavaScript Dates - Get Methods

    Good Tutorial +rep
    The owls are not what they seem...

  4. #4
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: JavaScript Dates - Get Methods

    As always, well done! +rep
    What prompted you to switch to JavaScript?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Getter and Setter Methods
    By ZekeDragon in forum C Tutorials
    Replies: 3
    Last Post: 09-20-2009, 05:38 PM
  2. Java Methods
    By chili5 in forum Java Tutorials
    Replies: 2
    Last Post: 08-28-2009, 09:47 AM
  3. Replies: 0
    Last Post: 11-27-2008, 01:30 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts