Mayeenul Islam
Mayeenul Islam

Reputation: 4762

JavaScript date() - for date-time clock

To show a Clock on the WordPress website, I'm using a simple code suggested at the following link:

I changed the display function to:

function display_ct() {
    var strcount
    var now = new Date();
    var d = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds() );
    // obtain local UTC offset and convert to msec
    localOffset = now.getTimezoneOffset() * 60000;
    var c = new Date(d + localOffset);
    document.getElementById('ct').innerHTML = c;
    tt=display_c();
}

So that it's showing:

Sun Jul 20 2014 14:57:47 GMT+0600 (Central Asia Standard Time)

The site is a Bangladesh-based site, so I need to show the time to be GMT+6 always. But I want to format the display like below:

Dhaka: Jul 20 2014 02:57:47 PM

I know I can show the string Dhaka:, but I can't modify the view of the JS to this. I've checked getFullYear(), getMonth() etc. but each time they are returning single value. How come the large output comes out from the date() function then? I din't check my code otherwise Dhaka.

I'm sorry, I'm novice in JavaScripting. :(

Upvotes: 0

Views: 806

Answers (1)

hityagi
hityagi

Reputation: 5256

Check the jsfiddle : http://jsfiddle.net/Ngs4v/

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];

var date = new Date();

//get mm dd yyyy
var mnth = monthNames[date.getMonth()];
var dt = date.getDate();
var yr = date.getFullYear();

//get hh mm ss
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hour = date.getHours();

//get AM/PM 
var ampm = hour >= 12 ? 'PM' : 'AM';
hour = hour % 12;
hour = hour ? hour : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;

$('#date').text('Dhaka : ' + mnth + ' ' + dt + ' '+ yr + ' ' + hour + ':'+ minutes + ':'+ seconds + ' ' + ampm);

Upvotes: 2

Related Questions