Javascript 获取上周的开始日期和最后日期
可能重复:Javascript 获取上周的开始日期和最后日期,javascript,Javascript,可能重复: 有谁能建议我如何通过给函数指定“今天”日期来获取上周的开始日期和结束日期吗?您可以使用 可能对你有用。你可以使用 也许它对你有用。获取一个月的最后一天: /** * Accepts either zero, one, or two parameters. * If zero parameters: defaults to today's date * If one parameter: Date object * If two parameters:
有谁能建议我如何通过给函数指定“今天”日期来获取上周的开始日期和结束日期吗?您可以使用 可能对你有用。你可以使用
也许它对你有用。获取一个月的最后一天:
/**
* Accepts either zero, one, or two parameters.
* If zero parameters: defaults to today's date
* If one parameter: Date object
* If two parameters: year, (zero-based) month
*/
function getLastDay() {
var year, month;
var lastDay = new Date();
if (arguments.length == 1) {
lastDay = arguments[0];
} else if (arguments.length > 0) {
lastDay.setYear(arguments[0]);
lastDay.setMonth(arguments[1]);
}
lastDay.setMonth(lastDay.getMonth() + 1);
lastDay.setDate(0);
return lastDay;
}
获取最后一个星期一:
/**
* Accepts same parameters as getLastDay()
*/
function getLastMonday() {
var lastMonday = getLastDay.apply(this, arguments);
lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay() == 0 ? 6 : (lastMonday.getDay() - 1)));
return lastMonday;
}
现在你可以做你的工作了
/**
* Accepts one parameter: Date object.
* Assumes start of week is Sunday.
*/
function getWeek(d) {
var jan1 = new Date(d.getFullYear(), 0, 1);
return Math.ceil((((d - jan1) / (24 * 60 * 60 * 1000)) + jan1.getDay() + 1) / 7);
}
然后使用
// Get the last week of this month:
var lastWeekThisMonth = getWeek(getLastDay());
Alert("lastWeekThisMonth: %s", lastWeekThisMonth);
获取当月的最后一天:
/**
* Accepts either zero, one, or two parameters.
* If zero parameters: defaults to today's date
* If one parameter: Date object
* If two parameters: year, (zero-based) month
*/
function getLastDay() {
var year, month;
var lastDay = new Date();
if (arguments.length == 1) {
lastDay = arguments[0];
} else if (arguments.length > 0) {
lastDay.setYear(arguments[0]);
lastDay.setMonth(arguments[1]);
}
lastDay.setMonth(lastDay.getMonth() + 1);
lastDay.setDate(0);
return lastDay;
}
获取最后一个星期一:
/**
* Accepts same parameters as getLastDay()
*/
function getLastMonday() {
var lastMonday = getLastDay.apply(this, arguments);
lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay() == 0 ? 6 : (lastMonday.getDay() - 1)));
return lastMonday;
}
现在你可以做你的工作了
/**
* Accepts one parameter: Date object.
* Assumes start of week is Sunday.
*/
function getWeek(d) {
var jan1 = new Date(d.getFullYear(), 0, 1);
return Math.ceil((((d - jan1) / (24 * 60 * 60 * 1000)) + jan1.getDay() + 1) / 7);
}
然后使用
// Get the last week of this month:
var lastWeekThisMonth = getWeek(getLastDay());
Alert("lastWeekThisMonth: %s", lastWeekThisMonth);
下面是工作代码!希望这能帮助任何人
var d = new Date();
var to = d.setTime(d.getTime() - (d.getDay() ? d.getDay() : 7) * 24 * 60 * 60 * 1000);
var from = d.setTime(d.getTime() - 6 * 24 * 60 * 60 * 1000);
alert(to);
alert(from);
下面是工作代码!希望这能帮助任何人
var d = new Date();
var to = d.setTime(d.getTime() - (d.getDay() ? d.getDay() : 7) * 24 * 60 * 60 * 1000);
var from = d.setTime(d.getTime() - 6 * 24 * 60 * 60 * 1000);
alert(to);
alert(from);
hsz@这一个是上周的,不是当前的周周,只需使用该脚本并在日期上添加-7。hsz@这一个是上周的,不是当前的周,只需使用该脚本并在日期上添加-7。我不想使用date.js,还有其他替代方案吗?请注意,我不想使用date.js,还有其他替代方法吗?请指教