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>
Tags:
Javascript | DHTML Tricks
Related posts
Comments
tsantos , on 3/6/2009 6:50:57 AM Said:
I do it slightly different, but get the same resultvar 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 , on 9/22/2009 5:57:57 AM Said:
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 , on 9/24/2009 10:23:24 PM Said:
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 , on 10/9/2009 3:27:55 PM Said:
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 , on 10/27/2009 8:54:14 AM Said:
Great post.
Miami web design , on 10/27/2009 8:59:06 AM Said:
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 , on 10/27/2009 8:58:46 PM Said:
Oh, I love javascript SO much! thanks for my daily dose! ;)
graco infant car seat , on 10/30/2009 10:36:40 PM Said:
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 , on 10/31/2009 11:47:56 PM Said:
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 , on 11/2/2009 2:41:53 AM Said:
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 , on 11/2/2009 11:26:39 AM Said:
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 , on 11/5/2009 6:30:41 AM Said:
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 , on 11/11/2009 3:40:54 PM Said:
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 , on 11/11/2009 6:07:26 PM Said:
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 , on 11/12/2009 10:50:13 PM Said:
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 , on 11/13/2009 10:20:57 PM Said:
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 , on 11/15/2009 1:40:32 AM Said:
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 , on 11/18/2009 1:49:10 AM Said:
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 , on 12/4/2009 11:49:35 PM Said:
Hi Very informative post. I usually read your post.. Keep blogging. Thanks http://babyscalereviews.com/
contractor accountants , on 12/12/2009 5:38:46 AM Said:
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 , on 12/15/2009 10:47:45 AM Said:
bridal gowns , on 12/21/2009 12:15:39 PM Said:
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 , on 1/1/2010 1:14:44 AM Said:
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 , on 1/4/2010 12:19:16 AM Said:
How tall should a tv Stand be for a 37 inch lcd tv?
adult cystic acne , on 1/6/2010 6:27:51 AM Said:
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:
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
Try, Strive and Struggle are the keys of my hope. When there is no hope, no success. So keep hoping and keep winning. No idea how true that statement is, as I myself is losing the biggest hope of mine. So do it on your own risk :)