月和年未显示 [英] Month and Year not displaying

查看:105
本文介绍了月和年未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示当前的月份和年份,但是什么也没有显示.

I'm trying to display the current month and year, but nothing is being displayed.

HTML:

<div id="date"></div>

CSS:

#date {
  display: block;
  color: black;
  font-size: 50px;
  top: 50px;
  left: 50px;
}

JavaScript:

(function() {
    var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

    var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

    Date.prototype.getMonthName = function() {
        return months[ this.getMonth() ];
    };
    Date.prototype.getDayName = function() {
        return days[ this.getDay() ];
    };
})();

var now = new Date();

var day = now.getDayName();
var month = now.getMonthName();

推荐答案

您好,请尝试使用此有效代码.

Hello Please try this is the working code.

JS

(function() {
    var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

    var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

    Date.prototype.getMonthName = function() {
        return months[ this.getMonth() ];
    };
    Date.prototype.getDayName = function() {
        return days[ this.getDay() ];
    };
})();

var now = new Date();
document.getElementById('date').innerHTML = now.getDayName() + "-" +  now.getMonthName();

HTML

<div id="date"></div>

这是 jsFiddle

这篇关于月和年未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆