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 Sum Function
Retrieving Data from Mysql/Mariadb
Java Script Basic

12 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!

  7. I’ve been exploring for a while for top quality articles or blogs on this
    kind of area. Exploring in Yahoo I at last came across this site.
    Looking over this info made me discovered just what I wanted.
    I will visit your site frequently.

  8. If your friends are going set for golf art prints and pictures
    of tiger woodsand Arnold palmer have you thought to just decorate your
    living room with one of these prints which are certainly going to result in the
    modification that you desire within your lounge. , you could possibly anticipate to see a
    few prints being bid as much as hundreds and hundreds of pounds.
    Lots of folks flourish enough to pay money on nice vacations,
    an extra home, a pleasant car, and even theater tickets.

  9. Fascinating post. I’ll be sticking around to find out a lot more
    from you fellas. Thank you!

  10. Thanks a lot for sharing this great info! I’m excited to see more
    blog posts!

  11. I do trust all the concepts you’ve offered to your
    post. They’re really convincing and can certainly work.

    Still, the posts are too short for novices.
    May you please prolong them a bit from next time? Thanks for the post.

  12. Admiring the dedication you put into your website and in depth information you offer.
    It’s great to come across a blog every once in a
    while that isn’t the same old rehashed information. Wonderful read!
    I’ve saved your site and I’m adding your RSS feeds to my Google account.

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
YouTube74
YouTube