内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>get last week in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get last week in javascript - Stack Overflow

programmeradmin0浏览0评论

I am using the following in a script:

var startDate = new Date("10/12/2012");
var endDate = new Date("10/18/2012");

I would like those dates to be dynamically created with startDate as last Monday and endDate as last Sunday. I have tried the following:

var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first)).format("m/dd/yyyy");
var endDate = new Date(curr.setDate(last)).format("m/dd/yyyy");

But for some reason this doesn't work - nothing is outputted for the startDate or endDate variables.

Any ideas what I am doing wrong?

I am using the following in a script:

var startDate = new Date("10/12/2012");
var endDate = new Date("10/18/2012");

I would like those dates to be dynamically created with startDate as last Monday and endDate as last Sunday. I have tried the following:

var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first)).format("m/dd/yyyy");
var endDate = new Date(curr.setDate(last)).format("m/dd/yyyy");

But for some reason this doesn't work - nothing is outputted for the startDate or endDate variables.

Any ideas what I am doing wrong?

Share Improve this question edited Nov 21, 2012 at 12:51 user18577 asked Nov 21, 2012 at 12:44 user18577user18577 6433 gold badges10 silver badges21 bronze badges 3
  • Please define what "this doesn't work" means. – Lee Taylor Commented Nov 21, 2012 at 12:47
  • Do you mean "the Monday before last"? So you're trying to get the Monday-> Sunday span preceding the current week? – jonvuri Commented Nov 21, 2012 at 12:49
  • Kiyura - yes that's what I mean – user18577 Commented Nov 21, 2012 at 12:51
Add a ment  | 

3 Answers 3

Reset to default 3

The Javascript datetime object does not have a format method. You'll need to use a library or generate the string yourself:

var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
startDate = "" + (startDate.getMonth() + 1) + "/" + startDate.getDate() + "/" + startDate.getFullYear();
var endDate = new Date(curr.setDate(last));
endDate = "" + (endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear();

Here's a fiddle http://jsfiddle/DPQeB/2/ and its output

11/18/2012
11/24/2012

One library that allows you to format dates is jQuery UI.

Using date.js to get last Sunday

Date.today().moveToDayOfWeek(0, -1); // -1 indicates to go back

Best to use a library to manipulate dates. It will make your life a lot easier.

Having said that have a long-term aim to understand dates in JavaScript. It will help you with things like debugging.

If starting from sunday:

var today = new Date();
var sundayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()-8);
var mondayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+1);

console.log( mondayOfWeek );
console.log( sundayOfWeek );
发布评论

评论列表(0)

  1. 暂无评论