function textGetDateTime(e) {
var datetime = e.value;//传入的某日期
//var date = new Date(datetime);//带参数的Date方法在IE浏览器中无法正常使用,因此用setUTCFullYear(year,month,day)方法来替换,该方法是设置时间的年-月-日部分,其中月和日为可选
var date = NewDate(datetime);//调用下面的函数
var weekDay = date.getDay(); //得到一周中的第几天(0~6)
if (weekDay == 0)
weekDay = 7;//将周日换算为7
var monthDay = date.getDate(); //得到一月中的第几天(1~31)
var startMonthDay = monthDay - weekDay + 1; //得到选择日期对应的某一周的周一是这个月的第几天
var stopMonthDay = monthDay + 7 - weekDay; //得到选择日期对应的某一周的周日是这个月的第几天
var startDate = new Date(datetime), stopDate = new Date(datetime);
startDate.setFullYear(date.getFullYear()); //年份
stopDate.setFullYear(date.getFullYear());
startDate.setMonth(date.getMonth()); //月份
stopDate.setMonth(date.getMonth());
startDate.setDate(startMonthDay); //一个月中的第几天
stopDate.setDate(stopMonthDay);
var newStartMonth = startDate.getMonth()
, newStopMonth = stopDate.getMonth()
, newStartDate = startDate.getDate()
, newStopDate = stopDate.getDate();//得到月份和日期
//判断月份和号数是否为单,如果为单就添加个0在前面
if (startDate.getMonth() + 1 < 10)
newStartMonth = "0" + (startDate.getMonth() + 1);
if (stopDate.getMonth() + 1 < 10)
newStopMonth = "0" + (stopDate.getMonth() + 1);
if (startDate.getDate() < 10)
newStartDate = "0" + startDate.getDate();
if (stopDate.getDate() < 10)
newStopDate = "0" + stopDate.getDate();
var newStartDate = startDate.getFullYear() + "-" + newStartMonth + "-" + newStartDate;
var newStopDate = stopDate.getFullYear() + "-" + newStopMonth + "-" + newStopDate;
}
function NewDate(str) {
str = str.split('-');
var date = new Date();
date.setUTCFullYear(str[0], str[1] - 1, str[2]);//设置年-月-日,其中月,日为可选
date.setUTCHours(0, 0, 0, 0);//设置时,分,秒,毫秒,其中分,秒,毫秒为可选
return date;
}
通过某日期得到该日期所在的一周的周一和周日的日期
最新推荐文章于 2023-05-10 22:48:49 发布