javaScript Date object

javascript date object enables storage and retrievel of dates and times.

the javascript date object provides uniform behavior across platforms.

the date objects are created with the date() constructor.

there are four ways to create a date:

           new    date()     // current  date  and time (utc)
           
           new    date(milliseconds)
           
           new    date(date_string)     // date_string format look like "month dd, yyyy hh:mm:ss"
           
           new    date(year,month,day,hours,minutes,seconds,milliseconds)

hours , minutes(0 to 59), seconds(0 to 59), ms(0 to 999) are all optional parameters.

year(full year) , month (0 for january) to (11 for december), and date(1 to 31) are required parameters.

All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.

var   today     =   new  date()    // date object initialized to the  current date and time
               
       var   date_1    =   new  date("november 22,2013  12:45:55")
               
       var   date_2    =   new  date(2014,5,12);         //object with date(yyyy,mm,dd).
               
       var   date_3    =   new  date(2014,5,12, 11,34,34)   //(yyyy,mm,dd,hh,mm,ss).

javascript date object parse() method

the parse() method parses a date string and returns the number of milliseconds between the date string and midnight of january 1, 1970.

example:
                   var    date_string   =  "11 25, 2013  11:48:48" ;
                   
                   var    date_milliseconds   =   date.parse(date_string);
                   
                   alert("__typeof__"+typeof  date_milliseconds+"___"+date_milliseconds);

note : if your given string is not parseable this method returns nan


JavaScript date object get() methods

javascript provides a comprehensive set of get methods to read each fields of a date.look in this example.

   var     today  =  new   date();
                  
   document.write('<br>'+'date.get'+'date()'+'___:'+today.getdate());
                 
   document.write('<br>'+'date.get'+'time()'+'___:'+today.gettime());
                 
   document.write('<br>'+'date.get'+'year()'+'___:'+today.getyear());
                 
   document.write('<br>'+'date.get'+'fullyear()()'+'___:'+today.getfullyear());
   
   document.write('<br>'+'date.get'+'month()'+'___:'+today.getmonth());
   
   document.write('<br>'+'date.get'+'day()'+'___:'+today.getday());
   
   document.write('<br>'+'date.get'+'hours()'+'___:'+today.gethours());
   
   document.write('<br>'+'date.get'+'minutes()'+'___:'+today.getminutes());
   
   document.write('<br>'+'date.get'+'seconds()'+'___:'+today.getseconds());
   
   document.write('<br>'+'date.get'+'milliseconds()'+':'+today.getmilliseconds());

coordinated universal time (utc) get methods

The Universal Coordinated Time (UTC) is the time set by the World Time Standard.

these methods return the field of a date according to universal time.

   var     today  =  new   date();
  
  
   with(document){
                  
   write('<br>'+'date.get'+'utc'+'date()'+'___:'+today.getutcdate());                
                 
   write('<br>'+'date.get'+'utc'+'fullyear()'+'___:'+today.getutcfullyear());
   
   write('<br>'+'date.get'+'utc'+'month()'+'___:'+today.getutcmonth());
   
   write('<br>'+'date.get'+'utc'+'day()'+'___:'+today.getutcday());
   
   write('<br>'+'date.get'+'utc'+'hours()'+'___:'+today.getutchours());
   
   write('<br>'+'date.get'+'utc'+'minutes()'+'___:'+today.getutcminutes());
   
   write('<br>'+'date.get'+'utc'+'seconds()'+'___:'+today.getutcseconds());
   
   write('<br>'+'date.get'+'utc'+'milliseconds()'+':'+today.getutcmilliseconds());
   
   }

javascript set dates methods

We can easily manipulate the date by using the set methods available for the Date object.

       var    date  =  new  date();
                
                       document.write("the today date object____"+date);
                
                       date.setFullYear(2014);
                       
                       date.setMonth(11);
                       
                       date.setHours(15);
                       
                       date.setMinutes(55);
                       
                       document.write("<br/>"+"after modify the date object"+date);

there are several facts to be aware of when working with javascript date values:-

  • javascript stores dates internally as the number of milliseconds the 'epoch' january 1st, 1970(gmt).this is an artifact of the way unix system store their time and can cause problems if you wish to work with dates prior to the epoch in older browerses.
  • when reading the current date and time, your script is at the mercy of the client machine's clock.if the client's date or time's is incorrect, you script will reflect this fact.
  • javascript date system days of the week and months of the year are enumerated beginning with zero.so day 0 is sunday, day 6 is saturday, month 0 is january, and month 11 is december.