web 2.0

Get Current Time Using JavaScript

Here is the quick code to get the current time in 12 hours format using JavaScript.

   1: <script language="javascript">
   2: function getTime() {
   3:     var dTime = new Date();
   4:     var hours = dTime.getHours();
   5:     var minute = dTime.getMinutes();
   6:     var period = "AM";
   7:     if (hours > 12) {
   8:         period = "PM"
   9:     }
  10:     else {
  11:         period = "AM";
  12:     }
  13:     hours = ((hours > 12) ? hours - 12 : hours)
  14:     return hours + ":" + minute + " " + period
  15: }
  16: </script>

Comments

tsantos , on 3/6/2009 6:50:57 AM Said:

tsantos

I do it slightly different, but get the same result

var getTime = function() {

var d = new Date();

d.setDate(new Date().getDate());

var time = d.toTimeString().substr(0, d.toTimeString().lastIndexOf(" "));

return (time + d.toLocaleTimeString().substr(d.toLocaleTimeString().lastIndexOf(" ")));

}

unblock drain United Kingdom, on 9/22/2009 5:57:57 AM Said:

unblock drain

I have recently set out to develop utilising Javascript in the client browser but  am finding it is a big learning curve.  The phrase " Get Current Time Using JavaScript " in this post, aroused my curiosity. My recent experience is with mysql, php, most linux based tools and a little flash with actionscript. The problems of employing Javascript to arrive at a good client script that runs quickly in most of the leading web browsers, IE, Safari, Firefox and Google Chrome is a big head ache which is taking me umpteen hours to solve.  I find the tutorial websites  are oftentimes inflexible and mention the identical points, dialog in blogs often reference actual methods to resolve problems which leads me through the learning curve more rapidly. Engrossing to read your thoughts and the remarks in your web site on Javascript. Thank you for the article, it has helped in a moderate way to get me through the learning curve.

sheartech United States, on 9/24/2009 10:23:24 PM Said:

sheartech

why should i take more time in getting time using java, when i can get it easily using my wrist watch LOL.

Raymond Weil watches United States, on 10/9/2009 3:27:55 PM Said:

Raymond Weil watches

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

waggi United States, on 10/27/2009 8:54:14 AM Said:

waggi

Great post.

Miami web design United States, on 10/27/2009 8:59:06 AM Said:

Miami web design

Nowadays mostly developers use Javascript because aside from it can use in open source this language are very effective. Each codes are very important and if one of them misinterpret your program doesn't work.

Weird Stories United States, on 10/27/2009 8:58:46 PM Said:

Weird Stories

Oh, I love javascript SO much! thanks for my daily dose! ;)

graco infant car seat United States, on 10/30/2009 10:36:40 PM Said:

graco infant car seat

Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

Personal Loans with No Credit Check United States, on 10/31/2009 11:47:56 PM Said:

Personal Loans with No Credit Check

You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.

fast weight loss program United States, on 11/2/2009 2:41:53 AM Said:

fast weight loss program

I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
Great stuff as usual...

DigBands.com United States, on 11/2/2009 11:26:39 AM Said:

DigBands.com

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!

Lyrics United States, on 11/5/2009 6:30:41 AM Said:

Lyrics

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

provillus liquid United States, on 11/11/2009 3:40:54 PM Said:

provillus liquid

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

at home weight loss United States, on 11/11/2009 6:07:26 PM Said:

at home weight loss

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.

flower delivery singapore United States, on 11/12/2009 10:50:13 PM Said:

flower delivery singapore

I completely agree with the above comment, the internet is with a doubt growing into the most important medium of communication across the globe and its due to sites like this that ideas are spreading so quickly.

Love Meter United States, on 11/13/2009 10:20:57 PM Said:

Love Meter

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

chiropractor charlotte United States, on 11/15/2009 1:40:32 AM Said:

chiropractor charlotte

I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

wedding photographer swansea United States, on 11/18/2009 1:49:10 AM Said:

wedding photographer swansea

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. Did you acquired lots of links and I see lots of trackbacks??

Baby Scale Singapore, on 12/4/2009 11:49:35 PM Said:

Baby Scale

Hi

Very informative post. I usually read your post.. Keep blogging.

Thanks
http://babyscalereviews.com/

contractor accountants United States, on 12/12/2009 5:38:46 AM Said:

contractor accountants

This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article

pool solar panels United States, on 12/15/2009 10:47:45 AM Said:

pool solar panels

I completely agree with the above comment, the internet is with a doubt growing into the most important medium of communication across the globe and its due to sites like this that ideas are spreading so quickly.

bridal gowns United States, on 12/21/2009 12:15:39 PM Said:

bridal gowns

I really love the way infomration presented in your post. I have added to you in my social bookmark…and i am w8ing ur next post.

Hidup Berani Untuk Gagal Australia, on 1/1/2010 1:14:44 AM Said:

Hidup Berani Untuk Gagal

Thanks for the post. I've been really interested in Flikr and how Yahoo uses those pictures in a lot of it's image search results.

monitor stands United Kingdom, on 1/4/2010 12:19:16 AM Said:

monitor stands

How tall should a tv Stand be for a 37 inch lcd tv?

adult cystic acne United States, on 1/6/2010 6:27:51 AM Said:

adult cystic acne

Have you ever wanted to try to treat your acne from the comfort of your home? Well there are in fact quite a number of home acne treatment methods available using the ingredients from your own kitchen. If you are a teen this may be ideal for you as a lot of what you need are already in the pantry and you can learn some ways treat your acne infections without going to your dermatologist.

Graco Nautilus 3 In 1 Multi Use Car Seat Bravo , on 4/29/2010 7:56:07 AM Said:

trackback

Graco Nautilus 3 In 1 Multi Use Car Seat Bravo

@ myphonyaccount , that’ s not the whole truth. Sure, a well designed light and small car CAN be better than a poorly designed big and heavy one. But a small car will always have a tougher crash if it meets a big and heavy car, that’ s just plain physics. Just look up the Audi Q7 vs. Fiat 500 crashtest. The Audi had 4 Euro- NCAP stars, the Fiat had 5. Yet, the passengers in the Audi would probably away without any serious injuries while the survival chance in the Fiat is quite small.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading