Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

JavaScript Date Object

In this article I will cover the concept of Date object in JavaScript. I will discuss the different methods and properties available with date object and also cover how to fetch the time details from Date object.

Introduction:

This tutorial will cover the basics of date object and then explain the different methods and property of date object.

Description:

The Date object is useful when you want to display a date or use a timestamp on page. In JavaScript date object is inbuilt data type. Once you created the object you can perform different kind of methods and property on the date object.

You can get and set the year, month, day, hour, minute, second, and millisecond fields of the date object by using different method. Date object uses either co-ordinated universal time (Universal/GMT) or local time for displaying date information.

According to ECMAScript (Standard of scripting language ) Date object can represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. I.e.This is a range of plus or minus 273,785 years, So the JavaScript can show the date and time till year 275755.

Basically Date object is used to work with dates and times. Date objects are created using “new Date ()” syntax. Date () object Accept these listed arguments.

[Var a=new date() ] - without any argument empty Date ( ) constructor creates a Date object set to the current date and time.

[ var a=new date(miliseconds) ]- After entering any numerical value date() object calculates it in millisecond value as returned by the getTime() method .

[ var a=new date(datestring) ] - When single string argument is passed, it is a string representation of a date.

[ var a=new date(year,month,date,hour,minute,second,millisecond) ] - Year: Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1992, rather than 92. Month: month, beginning with 0 for January to 11 for December. Date: day of the month. Hour: hour of the day (24-hour scale). Minute: minute segment of a time reading. Second: second segment of a time reading. Millisecond: Integer value representing the millisecond segment of a time reading.

JavaScript - Current Date:

JavaScript creates a Date object based on user's system clock and this is best way let JavaScript simply use the system clock. During creation of date object based on system clock it is important to note that if someone’s clock is off by a few hours or they are in other GMT then date object will give other times from the one created on your computer.

Let’s take some example to understand the concept of date object. If you are not passing any argument to the date constructer then it will create date object based on user’s system clock.

Listing 1: Script for date object

<html>
<head>
<title>MrBool Tutorials - JavaScript Dates</title>
<script type="text/javascript">

var vDate = new Date()
var vMonth = vDate.getMonth() + 1
var vDay = vDate.getDate()
var vYear = vDate.getFullYear()
document.write("Today's Date: " + vMonth + "/" + vDay + "/" + vYear);
document.write("</br>");
document.write("Today's Date: " + vDate);

</script>
</head>
<body >
</body>
</html>
 
Script to start date object

Figure 1: Above figure is output of Script to start date object.

These are the listed method which is supported by JavaScript .we can print more date information by using these method

  • getFullYear() - The four digit year (1970-9999)
  • getMonth() - Number of month (0-11)
  • getDate() - Day of the month (0-31)
  • getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday
  • getHours() - Number of hours (0-23)
  • getMinutes() - Number of minutes (0-59)
  • getSeconds() - Number of seconds (0-59)
  • getTime() - Number of milliseconds

JavaScript - Current Time:

Now instead of displaying the date, Let’s display the format like digital clock .

Listing 2: Script of digital clock

 <html>
<head>
<title>MrBool Tutorials - JavaScript Current Time</title>
</head>
<body>
<b>Current Time: 
<script type="text/javascript">
var objDate = new Date()
var objHours = objDate.getHours()
var objMinutes = objDate.getMinutes()
if (objMinutes < 10){
    objMinutes = "0" + objMinutes
}
document.write(objHours + ":" + objMinutes + " ")
if(objHours > 11){
   document.write("PM")
    } else {
      document.write("AM")
    }
</script>
</b>
</body>
</html>
showing current time in digital clock format

Figure 2: Above figure is showing current time in digital clock format.

It is important to know that if hours or minutes variable is less than 10 then we need to add a zero to the beginning of minutes. This is not necessary, but if it is 3:08 PM then it will display output "3.8 PM".

Conclusion:

This tutorial covered almost basic knowledge to deal with date object. Go through all the details and comment here in case of any questions.



I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login