duandu8202 2014-07-20 09:05 采纳率: 0%
浏览 47
已采纳

JavaScript date() - 用于日期时钟

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. :(

  • 写回答

1条回答 默认 最新

  • dtv995719 2014-07-20 10:01
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!