通过JavaScript获取一周中的特定日期的日期 [英] Get date of specific day of the week in JavaScript

查看:109
本文介绍了通过JavaScript获取一周中的特定日期的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我主要是脑屁。我只想快速转发一个 Date()直到一周的特定日期,然后获得该月份,日,年。



例如今天是09/03/10,但我想要一个功能( nextSession())返回09/08/10(下个星期三)。



我该怎么做?我可以想到的最好的事情是向 setDate()添加一天,直到 getDay() == 3,但是这是一个丑陋的...



PS jQuery是很酷的。

解决方案

  function nextSession(date){
var ret = new Date(date || new Date());
ret.setDate(ret.getDate()+(3 - 1 - ret.getDay()+ 7)%7 + 1);
return ret;
}

这个将在下个星期三设置日期。如果已经是星期三,结果将在给定日期后一周。



编辑:添加了没有参数调用函数的可能性: nextSession ) - 从今天起返回下一个会话日期


I think i'm mainly having a brain fart. I just want to fast forward a Date() until a specific day of the week and then get the Month, Day, Year for that.

E.g. today is 09/03/10 but i want a function (nextSession()) to return 09/08/10 (next wednesday).

How would I do this? The best thing I can think of is add a day to setDate() until getDay() == 3, but it's sorta ugly...

P.S. jQuery is cool too.

解决方案

function nextSession(date) {
    var ret = new Date(date||new Date());
    ret.setDate(ret.getDate() + (3 - 1 - ret.getDay() + 7) % 7 + 1);
    return ret;
}

This one will set date to next Wednesday. If it is already Wednesday, result will be one week later from given date.

EDIT: Added possibility of calling function without a parameter: nextSession() - it returns next session date from today

这篇关于通过JavaScript获取一周中的特定日期的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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