4

I took this code from PHP with javascript code, live clock , and successfully put it on my website. The code is here:

<?php
function d1() {
    $time1 = time();

    $date1 = date("h:i:sa",$time1);
    echo $date1;
}
?>
<script>
    var now = new Date(<?php echo time() * 1000 ?>);
    function startInterval(){
        setInterval('updateTime();', 1000);
    }
    startInterval();//start it right away
    function updateTime(){
        var nowMS = now.getTime();
        nowMS += 1000;
        now.setTime(nowMS);
        var clock = document.getElementById('qwe');
        if(clock){
            clock.innerHTML = now.toTimeString();//adjust to suit
        }
    }
</script>
<div id="qwe"></div>

However, the time format shows up as this:

21:41:44 GMT-0400 (Eastern Standard Time)

I want it to show up as

9:41:44 PM

I assume that this line

 clock.innerHTML = now.toTimeString();//adjust to suit

is the one I need to change, but can't find any formats that show it very simply. Do I need to write something up myself to show it in this custom format?

2
  • 2
    stackoverflow.com/questions/1056728/… this should solve your problem Commented Aug 17, 2015 at 1:49
  • thank you, thank you, I can't believe I didn't find this before Commented Aug 17, 2015 at 1:52

2 Answers 2

5

I want to suggest you try to use Developer tools in Chrome so you could examine what are the properties and functions available for an object.

Try this, F12 to the developer tool and in there type

var date_new = new Date()

then type date_new. after pressing that dot, you'll see what potential functions are available to you

enter image description here

Of course, its good to read on official documentation to know all about this, but in case you don't have internet connection or in a hurry. you can just look around at those functions of Date Object.

Have fun coding!

Sign up to request clarification or add additional context in comments.

3 Comments

Btw momentJs is also easy to use and quite lightweight already. But sometimes pure JavaScript rocks too :)
I tried momentJs, for some reason it isn't working I have it here jsfiddle.net/0qze164t can you help me identify what is wrong here? did I incorrectly download moment.js?
Code school has a free course on devTools for chrome. codeschool.com/courses/discover-devtools
1

You could try and do something like this.

function formatTime(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  var ampm = hours >= 12 ? 'PM' : 'AM';

  hours = hours % 12;
  hours = hours ? hours : 12; 

  minutes = minutes < 10 ? '0'+minutes : minutes;
  return hours + ':' + minutes + ':' + seconds + ' ' + ampm;
}

// your existing code with modification
...
clock.innerHTML = formatTime(now);//adjust to suit
...

If you happen to be working with date time too much and are having to use functions like above, you should consider using momentjs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.