![](/img/trans.png)
[英]Generate all dates of a week using current date also show week starting date on Monday and weekends on Sunday
[英]Simple way to show the current week using jquery
我需要在頁面上有一個腳本,該腳本可以動態更改當前星期。 我需要表演“ 2010年2月15日這一周。
在下一個星期日之后,我希望頁面上顯示“ 2010年2月22日當周”。 我選擇星期一作為展示日,但很可能是星期日。
任何建議,不勝感激!
本質上不需要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
可能只需要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包含星期幾。
嘗試這個:
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.