我正在構(gòu)建我的第一個(gè)javascript,因?yàn)槲以谶@里和許多網(wǎng)站上搜索解決方案,但找不到任何人適合我的情況。
我需要的是顯示當(dāng)前日期值到一個(gè)具有類名、日名和月名(自定義)的div中。但是div顯示的當(dāng)前日期是這樣的:123年6月6日,所以年份是錯(cuò)誤的,日期名稱也是錯(cuò)誤的
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getYear();
var dia = today.getDay();
var display = days[day] + " " + day + " " + months[month] + " " + year;
$('.first-date').html(display);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="first-date"></span>
getYear現(xiàn)在已被棄用,因此您需要像下面這樣使用getFullYear:
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var today = new Date();
var day = today.getDate();
var dayForText = today.getDay();
var month = today.getMonth();
var year = today.getFullYear();
var dia = today.getDay();
var display = days[dayForText] + " " + day + " " + months[month] + " " + year;
$('.first-date').html(display);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="first-date"></span>