繁体   English   中英

使用 JavaScript 在 html 页面中显示带有 CST 时间的时钟

[英]Show clock with CST time in html page using JavaScript

如何使用 JavaScript 在 HTML 页面中以 CST 时间格式显示时钟? 它应该以这种格式显示准确的 CST 时间“02 Jul 2015 10:34:45 PM”

不确定是否有更好的方法,但我找不到任何东西。 这虽然有效。

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

d = new Date();
    //set the offset
    var utcOffset = -6;
    //get utc
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    //create new date adjusting by the utcOffset to get local date/time
    var localDate = new Date(utc + (3600000 * (utcOffset+1)));
    //check if one character if so format as two by adding leading 0.
    var formatDay = (localDate.getDate().length > 1) ? localDate.getDate() : '0' + localDate.getDate();
    //string everything together
    var fullDate = formatDay + ' ' + monthNames[localDate.getMonth()] + ' ' + localDate.getFullYear() + ' ' + localDate.toLocaleTimeString();

$('#div1').html(fullDate);

http://jsfiddle.net/ojga6a5u/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM