JavaScript date() - 用于日期时钟

时间:2014-07-20 09:05:12

标签: javascript php

要在WordPress网站上显示时钟,我使用以下链接建议的简单代码:

我将显示功能更改为:

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();
}

所以它显示:

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

该网站是一个位于孟加拉国的网站,因此我需要始终显示时间GMT+6。但我想格式化显示如下:

Dhaka: Jul 20 2014 02:57:47 PM

我知道我可以显示字符串Dhaka:,但我无法修改JS的视图。我已经检查了getFullYear()getMonth()等,但每次都返回单个值。那么大输出怎么来自date()函数呢?除非达卡,否则我不会检查我的代码。

对不起,我是JavaScripting的新手。 :(

1 个答案:

答案 0 :(得分:2)

检查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);