Javascript has a lot of inbuilt methods you can use to get the date and time. The first thing you need to do is to create a new Date object:

var d = new Date();

Example to get the current date:

<script>

var d = new Date();

document.write(d);

var d1=d.getDay();

var m=d.getMonth()+1;

var y=d.getFullYear();

document.write(“Today is “ + d1 + “/” +m + “/” +y);

</script>

Example – Age Calculator
<html>

<head>

<script>

Function age() {

Var byear =prompt(“enter Year of Birth ?”);

var d = new Date();

var y=d.getFullYear();

var q=y-byear;

alert(“You are  “+q + “ Years Old”);

}  </script>

</head>

<body>

<h2>Age Calculator Example </h2>

<input type=”button” value=”Calculate Age” onclick=”age()”>

</body>

</html>

Week Days Example – Switch Case

<script>

function viewday()

{

var d=new Date().getDay();

switch(d)

{

case 0:

alert(“sunday”);

break;

case 1:

alert(“Monday”);

break;

case 2:

alert(“Tuesday”);

break;

case 3:

alert(“Wednesday”);

break;

case 4:

alert(“Thursday”);

break;

case 5:

alert(“Friday”);

break;

default:

alert(“Saturday”);

break;

}

}

</script>

<input type=”button” value=”Get Day” onclick=” viewday()”>

author image

About admin

You Might Also Like...

Excel Macro Training Details
Retrieving Data from Mysql/Mariadb

6 Comments

  1. Hello there! Nice stuff, please keep me posted any time you post
    something just like this!

  2. Hi and thank you for the actual blog post ive recently been searching about this particular advice on-line for sum hours these days.
    many thanks.

  3. You have mentioned very attention-grabbing things. Thank you for sharing.

  4. Hello friend, This was a fantastic post for such a difficult topic to talk about.
    I look ahead to seeing a lot more superb posts like this one.

    Thank you.

  5. Keep up the wonderful piece of work, I read few posts
    on this site and I think that your web blog is truly
    attention-grabbing as well as has lots of outstanding information.

  6. I don’t know how I ended up here, however I thought this post was great.
    I do not know who you are but definitely you’re going to a famous blogger when you aren’t already
    Many thanks!

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/04/25/javascript-dates-time
YouTube68
YouTube