简体   繁体   English

使用jQuery显示当前星期的简单方法

[英]Simple way to show the current week using jquery

I need to have a script on a page which dynamically changes the current week. 我需要在页面上有一个脚本,该脚本可以动态更改当前星期。 I need to have the show "Week of February 15, 2010. 我需要表演“ 2010年2月15日这一周。

After next Sunday, I'd like the page to say "Week of February 22, 2010". 在下一个星期日之后,我希望页面上显示“ 2010年2月22日当周”。 I picked Monday for the display day but it could easily be Sunday. 我选择星期一作为展示日,但很可能是星期日。

Any suggestions are much appreciated!. 任何建议,不胜感激!

No jQuery inherently required. 本质上不需要jQuery。

var date = new Date();
var dayOfWeek = date.getDay();
date.setTime(date.getTime() - dayOfWeek*86400000); // 86 400 000 = # MS per day
alert('Week of ' + date.toLocaleFormat('%B %e, %Y') + '.'); // toLocaleFormat is a non-standard method

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date

jQuery is needed maybe just to easily show the result, but date functions are core javascript: 可能只需要jQuery即可轻松显示结果,但date函数是核心javascript:

var date = new Date();
while (date.getDate() != 1 /* Monday */) {
    date.setDate(date.getDate() - 1);
}
var months = new Array("January", "February", "March", "April", 
                       "May", "June", "July", "August", "September", 
                        "October", "November", "December");

$("#some-id").val("Week of " + months[date.getMonth()] + " " +
                  date.getDate() + ", " + date.getFullYear());

var d=new Date();
var m=new Date(d.getTime() - 24*60*60*1000*d.getDay())
document.write(m);

m contains date of Sunday of this week. m包含星期几。

Try this: 尝试这个:

const getCurrentWeek = (function() {
    let fdWeek = new Date();
    let dia = fdWeek.getDay();
    let firtDay = new Date();
    firtDay.setTime(fdWeek.setUTCHours(-((dia) * 24)));
    let lastDay = new Date();
    lastDay.setTime(fdWeek.setUTCHours(6 * 24));
    let week = {
        firtDay : firtDay,
        lastDay : lastDay
    }
    return week;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM