js:
//第一种,测试可使用
function getWeekDate(n){
var now=new Date();
var year=now.getFullYear();
//因为月份是从0开始的,所以获取这个月的月份数要加1才行
var month=now.getMonth()+1;
var date=now.getDate();
var day=now.getDay();
//判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
if(day!==0){
n=n+(day-1);
}
else{
n=n+day;
}
if(day){
//这个判断是为了解决跨年的问题
if(month>1){
month=month;
}
//这个判断是为了解决跨年的问题,月份是从0开始的
else{
year=year-1;
month=12;
}
}
now.setDate(now.getDate()-n);
year=now.getFullYear();
month=now.getMonth()+1;
date=now.getDate();
s=year+"年"+(month<10?('0'+month):month)+"月"+(date<10?('0'+date):date)+"日";
return s;
}
//设置本周开始结束日期
$('#current_week').html(getWeekDate(0) + "~" + getWeekDate(-6));
///第二种(未测试)
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();
//格式化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth()+1;
var myweekday = date.getDate();
if(mymonth < 10){
mymonth = "0" + mymonth;
}
if(myweekday < 10){
myweekday = "0" + myweekday;
}
return (myyear+"-"+mymonth + "-" + myweekday);
}
//获得本周的开始日期
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}
//获得本周的结束日期
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}
/** java
* 获取当前日期所在周第一天和最后一天的日期
* @return
*/
public static Map<String, String> getWeekDay() {
Map<String, String> map = new HashMap<String, String>();
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
map.put("mon", df.format(cal.getTime()));
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, 1);
map.put("sun", df.format(cal.getTime()));
return map;
}
/**
* 获取指定日期所在周第一天和最后一天的日期
* @param currentDate
* @return
* @throws ParseException
* @throws java.text.ParseException
*/
public static Map<String, String> getWeekDay(String currentDate) throws ParseException{
Map<String, String> map = new HashMap<String, String>();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(currentDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
map.put("mon", df.format(cal.getTime()));
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, 1);
map.put("sun", df.format(cal.getTime()));
return map;
}