获取当前周moday javascript [英] get current week moday javascript

查看:76
本文介绍了获取当前周moday javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据今天的时间获取当前的星期一",示例为:

i want to get current week 'Monday' based on today's time, samples are:

2017年11月21日的输出应为2017年11月20日

21-11-2017 output should be 20-11-2017

2017年11月23日的输出应为2017年11月20日

23-11-2017 output should be 20-11-2017

2017年11月26日的输出应为2017年11月20日

26-11-2017 output should be 20-11-2017

var d = new Date();
 console.log(d.getDate())
 console.log(d.getDay()+1)
d.setDate(d.getDate() - d.getDay()+1);

除了星期天(2017年11月26日)手动更改时间以测试返回的不同案例之外,该代码正常工作

the code is working fine except for sunday (26-11-2017) manaully changed time to test different cases it returns

Mon Nov 27 2017 23:50:39 GMT-0800 (Pacific Standard Time) 

对于其他日期,例如

2017年11月25日,返回2017年11月20日星期一格林尼治标准时间0800(太平洋标准时间)

25-11-2017 it return Mon Nov 20 2017 23:50:39 GMT-0800 (Pacific Standard Time)

2017年11月24日返回2017年11月20日星期一格林尼治标准时间0800(太平洋标准时间)

24-11-2017 it return Mon Nov 20 2017 23:50:39 GMT-0800 (Pacific Standard Time)

2017年11月22日,返回2017年11月20日星期一格林尼治标准时间0800(太平洋标准时间) 这是理想的选择,但每个星期天它都会返回即将到来的星期一,我想不出什么好东西

22-11-2017 it return Mon Nov 20 2017 23:50:39 GMT-0800 (Pacific Standard Time) that is desired but for EVERY sunday it returns the upcoming monday i couldn't figure out something good

简而言之,我想从星期一开始而不是从星期日开始

In short i want to start my week from monday not from sunday

推荐答案

使用setDate而不是setDay来更改实例中的日期.循环播放,直到您到达星期一为止:

Use setDate rather than setDay to change the date in the instance. Loop until you get to Monday:

var dt = new Date();
while (dt.getDay() != 1) {
  dt.setDate(dt.getDate() - 1);
}
console.log(dt.toString());

开始日期的示例:

test("19-11-2017");
test("20-11-2017");
test("21-11-2017");
test("22-11-2017");
test("23-11-2017");
test("24-11-2017");
test("25-11-2017");
test("26-11-2017");
function findMonday(dt) {
  while (dt.getDay() != 1) {
    dt.setDate(dt.getDate() - 1);
  }
  return dt;
}
function test(str) {
  var parts = str.split("-");
  var dt = findMonday(new Date(+parts[2], +parts[1] - 1, +parts[0]));
  console.log("Start: " + str + ", found: " + dt.toString());
}

.as-console-wrapper {
  max-height: 100% !important;
}

或者稍微更有效(这无关紧要,除非您在紧密的循环中进行了数十万次此操作),以找出要返回多少天一次:

Or it's slightly more efficient (nothing that's going to matter unless you're doing this hundreds of thousands of times in a tight loop) to figure out how many days back to go and go all at once:

var dt = new Date();
var days = ((dt.getDay() + 7) - 1) % 7;
dt.setDate(dt.getDate() - days);
console.log(dt.toString());

开始日期的示例:

test("19-11-2017");
test("20-11-2017");
test("21-11-2017");
test("22-11-2017");
test("23-11-2017");
test("24-11-2017");
test("25-11-2017");
test("26-11-2017");
function findMonday(dt) {
    var days = ((dt.getDay() + 7) - 1) % 7;
    dt.setDate(dt.getDate() - days);
  return dt;
}
function test(str) {
  var parts = str.split("-");
  var dt = findMonday(new Date(+parts[2], +parts[1] - 1, +parts[0]));
  console.log("Start: " + str + ", found: " + dt.toString());
}

.as-console-wrapper {
  max-height: 100% !important;
}

这篇关于获取当前周moday javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆