Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

Date and Time Object

The Date object is useful when you want to display a date or use a timestamp in some sort of calculation. In Java, you can either make a Date object by supplying the date of your choice, or you can let JavaScript create a Date object based on your visitor's system clock. It is usually best to let JavaScript simply use the system clock.

Get the JavaScript Time
The Date object has been created, and now we have a variable that holds the current date! To get the information we need to print out, we have to utilize some or all of the following functions:

  1. getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM
  2. getSeconds() - Number of seconds (0-59)
  3. getMinutes() - Number of minutes (0-59)
  4. getHours() - Number of hours (0-23)
  5. getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday
  6. getDate() - Day of the month (0-31)
  7. getMonth() - Number of month (0-11)
  8. getFullYear() - The four digit year (1970-9999)

 

Example:
<script>
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" +  year)
</script>

Output :
It will print the current date.


 

Notice that we added 1 to the month variable to correct the problem with January being 0 and December being 11. After adding 1, January will be 1, and December will be 12

Example:
<script>
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
   minutes = "0" + minutes
}
document.write(hours + ":" + minutes + "  ")
if(hours > 11){
   document.write("PM")
} else {
document.write("AM")
}
</script>

Output :

 
     
   
JavaScript Tutorial
 
  Introduction
Introduction
  Object Oriented Programming
Object Oriented Programming
  How to
How to
  Where use JavaScript
Where use JavaScript
  Variables
Variables
  Operators
Operators
  if Statement
if Statement
  Switch Statement
Switch Statement
  Functions
Functions
  for Loop
for Loop
  while Loop
while Loop
  Popup Boxes
Popup Boxes
  break and continue Statements
break and continue Statements
  Events
Events
  Date and Time Object
Date and Time Object
  Special Characters
Special Characters
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download Mail List Cash Extraction  GoldenMine Ebook Mail List Cash Extraction GoldenMine
   
Download Algorithms for Programmers  Ebook Algorithms for Programmers
   
Download The Affiliate Marketers Master Guide Ebook The Affiliate Marketers Master Guide
   
Download PPC  Pay Per Click Google AdWords Made Easy  Ebook PPC Pay Per Click Google AdWords Made Easy
   
Download PHP Essentials  Ebook PHP Essentials
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz