jQuery,格式化日期未显示正确的格式
jQuery, format date doesn't showing the right format
我想显示所选月份第一天的日期,但日期格式不正确。
我想这样显示01/11/2017但它显示成这样
Wed Nov 01 2017 00:00:00 GMT+0100 (CET)
Select a Date: <input type="date" id="theDate" onmouseout="getTheDays()"/>
<p>First Day of the Month: <label id="fday"></label></p>
<p>Last Day of the Month: <label id="lday"></label></p>
<script>
function getTheDays() {
var dt = new Date($('#theDate').val());
// GET THE MONTH AND YEAR OF THE SELECTED DATE.
var month = dt.getMonth(),
year = dt.getFullYear();
// GET THE FIRST AND LAST DATE OF THE MONTH.
var FirstDay = new Date(year, month, 1);
var LastDay = new Date(year, month + 1, 0);
// FINALLY, GET THE DAY.
var weekday = new Array();
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
if (typeof weekday[FirstDay.getDay()] != 'undefined') { // CHECK FOR 'undefined'.
$('#fday').html(FirstDay.toString('dd-MM-yy'));
}
else {
$('#fday').text('');
$('#lday').text('');
}
}
</script>
我和你分享我们的toShortDate
功能,给你:
function toShortDate(date) {
if (!date) {
return '--/--/--';
}
var d = new Date(Date.parse(date)),
days = d.getDate(),
month = (d.getMonth() + 1),
finalDays = days < 10 ? '0' + days : '' + days,
finalMonth = month < 10 ? '0' + month : '' + month;
return finalDays +
"/" + finalMonth +
"/" + d.getFullYear().toString().substr(2, 2);
}
此功能已经在生产环境中运行了一段时间,并且运行良好。祝你好运。
我想显示所选月份第一天的日期,但日期格式不正确。
我想这样显示01/11/2017但它显示成这样
Wed Nov 01 2017 00:00:00 GMT+0100 (CET)
Select a Date: <input type="date" id="theDate" onmouseout="getTheDays()"/>
<p>First Day of the Month: <label id="fday"></label></p>
<p>Last Day of the Month: <label id="lday"></label></p>
<script>
function getTheDays() {
var dt = new Date($('#theDate').val());
// GET THE MONTH AND YEAR OF THE SELECTED DATE.
var month = dt.getMonth(),
year = dt.getFullYear();
// GET THE FIRST AND LAST DATE OF THE MONTH.
var FirstDay = new Date(year, month, 1);
var LastDay = new Date(year, month + 1, 0);
// FINALLY, GET THE DAY.
var weekday = new Array();
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
if (typeof weekday[FirstDay.getDay()] != 'undefined') { // CHECK FOR 'undefined'.
$('#fday').html(FirstDay.toString('dd-MM-yy'));
}
else {
$('#fday').text('');
$('#lday').text('');
}
}
</script>
我和你分享我们的toShortDate
功能,给你:
function toShortDate(date) {
if (!date) {
return '--/--/--';
}
var d = new Date(Date.parse(date)),
days = d.getDate(),
month = (d.getMonth() + 1),
finalDays = days < 10 ? '0' + days : '' + days,
finalMonth = month < 10 ? '0' + month : '' + month;
return finalDays +
"/" + finalMonth +
"/" + d.getFullYear().toString().substr(2, 2);
}
此功能已经在生产环境中运行了一段时间,并且运行良好。祝你好运。