js判断两个输入日期为同一周的

yulei518 2010-09-19 10:21:55
请哪位高手,用JavaScript写一个输入两个日期,判断为同一周的。
不是为同一周的验证,谢谢!
...全文
595 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
晚安蕾欧娜. 2021-07-26
  • 打赏
  • 举报
回复

        val = ['2021-07-26', '2021-07-30']
        const olddate = new Date(val[0]).getDay() === 0 ? 7 : new Date(val[0]).getDay()
        const newdate = new Date(val[1]).getDay() === 0 ? 7 : new Date(val[1]).getDay()
        const interval = newdate - olddate
        console.log('时间1', val[0])
        console.log('时间2', val[1])
        console.log('时间1->是周几', olddate)
        console.log('时间2->是周几', newdate)
        console.log('周->间隔', interval)
        console.log('日期间隔', window.dayjs(val[1]).diff(window.dayjs(val[0]), 'day'))
        if (interval === window.dayjs(val[1]).diff(window.dayjs(val[0]), 'day')) {
          console.log('同一周')
        } else {
          console.error('不是同一周')
          }

需要引用Day.js
连接:链接标题

_loehuang_ 2010-09-20
  • 打赏
  • 举报
回复


<script type="text/javascript">


function exports(startime,endTime){
var longStartTime = simpleDateFormat_Parse(startime);
var longEndTime = simpleDateFormat_Parse(endTime);

var interval = longEndTime - longStartTime;

if(interval > 7*24*3600*1000){
alert("间隔时间大于一周");
}else{
alert("间隔时间小于一周");
}
}

/**
* 解析 日期 将 格式为 yyyy-MM-dd HH:mm:ss 转成 long类型的数值
*/
function simpleDateFormat_Parse(dateString) {
var dateArray = dateString.split("-");
var sYear = dateArray[0];
var sMonth = dateArray[1];
var dateArray2 = dateArray[2].split(" ");
var sDay = dateArray2[0];
var dateArray3 = dateArray2[1].split(":");
var sHour = dateArray3[0];
var sMinute = dateArray3[1];
var sSecond = dateArray3[2];

return new Date(sYear, sMonth - 1, sDay, sHour, sMinute,sSecond).getTime();
}
var date1 = "2010-9-19 19:30:30";

var date2 = "2010-9-20 19:30:30";

exports(date1,date2);

</script>



思路就是这样!
MrSLin 2010-09-20
  • 打赏
  • 举报
回复

function getWeekDay(year,month,day)
{
var baseDay=new Date(2010,9,19);
var day=new Date(year,month,day);
return Math.ceil((day.getTime()-baseDay.getTime())/(7*24*3600*1000));
}

getWeekDay(2010,9,19)==getWeekDay(2010,9,12)?alert('in the same week'):alert('not in the same week');
getWeekDay(2010,9,16)==getWeekDay(2010,9,21)?alert('in the same week'):alert('not in the same week');
getWeekDay(2010,12,30)==getWeekDay(2011,1,1)?alert('in the same week'):alert('not in the same week');
阿杰心路历程 2010-09-20
  • 打赏
  • 举报
回复

<script language="javascript">


function check(old,now){
var now_time = now.getTime();
var old_time = old.getTime();
var now_day = now.getDay()==0?7:now.getDay();
var old_day = old.getDay()==0?7:old.getDay();

if(now_time >= old_time+7*60*60*24*1000 || old_time >=now_time+7*60*60*24*1000){
alert('不属于本星期内的');
return false;
}else if(old.getDate() == now.getDate() ||old.getDate()>now.getDate&&old_day>new_day||old.getDate()

<now.getDate&&old_day < new_day ){
alert('符合楼主要求!');
return true;
}
alert('不属于本星期内的,属于7天内的!');
return false;

}
var a = Date.parse('9/19/2010');
var date = new Date(a);
var now = new Date();
check(date,now);
</script>
阿杰心路历程 2010-09-20
  • 打赏
  • 举报
回复
上面说错了 是(总天数+4)/7
阿杰心路历程 2010-09-20
  • 打赏
  • 举报
回复 1

<script language="javascript">
function check(old,now){
var oneDayTime = 1000*60*60*24;
var old_count =parseInt(old.getTime()/oneDayTime);
var now_other =parseInt(now.getTime()/oneDayTime);
return parseInt((old_count+4)/7) == parseInt((now_other+4)/7);
}
//var date = new Date(Date.parse('9/20/2010'));
var date = new Date();
var a = Date.parse('9/19/2010');
var now = new Date(a);
alert(check(date,now));
</script>

昨晚睡觉的时候又想到的一个简单的算法,原理就是先计算出 现在距离1970年1月1日的总天数
因为1970年1月1 是周4 所以(天数+7)/7 取整 就是周数 如果相同就是同一周反之就不是

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧