我想在2019年5月9日获得当前时间。
var d = new Date();
var delay = 500;
var month = d.getMonth()+1;
var day = d.getDate();
var y=d.getFullYear();
var output = d.getFullYear() + '/' +
((''+month).length<2 ? '0' : '') + month + '/' +
((''+day).length<2 ? '0' : '') + day;
在这里,输出显示了2019/05/09格式的当前时间。如何获得" 2019年5月5日"格式。
尝试此
var d = new Date();
var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var month = months[d.getMonth()]
var day = d.getDate();
var y=d.getFullYear();
var today = month +" " +day + "," + y;
console.log(today);
,而不是创建(并维护(自己的名称数组,您可以使用以下内容:
d.toLocaleString('en-us', { month: 'long' });
var d = new Date();
var month = d.toLocaleString('en-us', { month: 'long' });
var day = d.getDate();
var y=d.getFullYear();
var today = month +" " +day + "," + y;
console.log(today);
您可以做到这一点,
var monthNames = [
"Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct",
"Nov", "Dec"
];
var d = new Date();
var day = d.getDate();
var monthIndex = d.getMonth();
var year = d.getFullYear();
var output = monthNames[monthIndex] + ' 'day + ' ,' + year;
这正是您要寻找的
function LogCurrentDate(e){
var date = new Date();
var options = { year: 'numeric', month: 'long', day: '2-digit' };
$(".output").empty().append("<p>" + date.toLocaleDateString('en-US', options) + "</p>");
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<button onclick="LogCurrentDate(event)">Show Date</button>
</div>
<div class="output"></div>