[英]JavaScript: How to get dates for seven days ago, last month, this week
[英]Calculate last week start date in JavaScript (not -7 days ago)
我有一个活动记录列表,这些记录都有一个日期并显示在活动流中。
我想在记录之间插入一个分隔符DIV,这取决于它们基于recordsa创建日期所属的日期组。
我想显示带有本周文本的DIV, 该文本将采用当前星期的开始日期(不超过7天),然后我可以显示自该日期以来创建的所有记录。
我也想为上周做一个,这意味着我需要获取上周的开始日期和上周的结束日期的日期值。 或开始日期+7就可以了。
然后,我有了一个JavaScript库来比较日期范围。 我可以输入开始日期和结束日期,并确定第三个日期是否在该日期范围内,这会让我知道在每个“日期”部分下显示哪些记录。
所以我在计算:时寻求帮助
一周的开始日期是大多数日历或星期一的星期日
另外,如果可以的话,我宁愿不要对这些少量的日期操作使用庞大的Moment.js库。
谢谢你的帮助
下图显示了日期部分要打破的记录,我正在尝试实现的目标...
日期库,用于DateTime比较,并检查日期是否在2个日期之间!
// Source: http://stackoverflow.com/questions/497790
var dates = {
convert: function(d) {
// Converts the date in d to a date-object. The input can be:
// a date object: returned without modification
// an array : Interpreted as [year,month,day]. NOTE: month is 0-11.
// a number : Interpreted as number of milliseconds
// since 1 Jan 1970 (a timestamp)
// a string : Any format supported by the javascript engine, like
// "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
// an object : Interpreted as an object with year, month and date
// attributes. **NOTE** month is 0-11.
return (
d.constructor === Date ? d :
d.constructor === Array ? new Date(d[0], d[1], d[2]) :
d.constructor === Number ? new Date(d) :
d.constructor === String ? new Date(d) :
typeof d === "object" ? new Date(d.year, d.month, d.date) :
NaN
);
},
compare: function(a, b) {
// Compare two dates (could be of any type supported by the convert
// function above) and returns:
// -1 : if a < b
// 0 : if a = b
// 1 : if a > b
// NaN : if a or b is an illegal date
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(a = this.convert(a).valueOf()) &&
isFinite(b = this.convert(b).valueOf()) ?
(a > b) - (a < b) :
NaN
);
},
inRange: function(d, start, end) {
// Checks if date in d is between dates in start and end.
// Returns a boolean or NaN:
// true : if d is between start and end (inclusive)
// false : if d is before start or after end
// NaN : if one or more of the dates is illegal.
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(d = this.convert(d).valueOf()) &&
isFinite(start = this.convert(start).valueOf()) &&
isFinite(end = this.convert(end).valueOf()) ?
start <= d && d <= end :
NaN
);
},
// Subtract number of months from current month
// dates.subtractMonth(1)
subtractMonth: function(numberOfMonths) {
//var d = this;
var d = new Date();
d.setMonth(d.getMonth() - numberOfMonths);
d.setDate(1);
return d;
}
};
///////////////////////////////////////////////////////////////////////////
我的示例JSON数据,其日期将与其他日期进行比较,以确定何时将日期节头注入循环中
var task_activities = [{"id":1,"name":"record 1","date_time":"1\/5\/2015"},{"id":2,"name":"record 2","date_time":"1\/9\/2015"},{"id":3,"name":"record 3","date_time":"1\/13\/2015"},{"id":4,"name":"record 4","date_time":"1\/17\/2015"},{"id":5,"name":"record 5","date_time":"1\/21\/2015"},{"id":6,"name":"record 6","date_time":"1\/25\/2015"},{"id":7,"name":"record 7","date_time":"1\/29\/2015"},{"id":8,"name":"record 8","date_time":"2\/1\/2015"},{"id":9,"name":"record 9","date_time":"2\/5\/2015"},{"id":10,"name":"record 10","date_time":"2\/9\/2015"},{"id":11,"name":"record 11","date_time":"2\/13\/2015"},{"id":12,"name":"record 12","date_time":"2\/17\/2015"},{"id":13,"name":"record 13","date_time":"2\/21\/2015"},{"id":14,"name":"record 14","date_time":"2\/25\/2015"},{"id":15,"name":"record 15","date_time":"2\/29\/2015"},{"id":16,"name":"record 16","date_time":"3\/1\/2015"},{"id":17,"name":"record 17","date_time":"3\/5\/2015"},{"id":18,"name":"record 18","date_time":"3\/9\/2015"},{"id":19,"name":"record 19","date_time":"3\/13\/2015"},{"id":20,"name":"record 20","date_time":"3\/17\/2015"}];
// Loop over each Task Activity record and generate HTML
$.each(task_activities, function(i, activity) {
console.log(activity.date_time);
}); // end each
首先, subtractMonth函数存在缺陷,对于3月31日减1个月,它返回3月1日。
如果要查找给定日期的一周的第一天,只需从日期中减去从星期日开始的几周的天数,或者减去一个少一点的日期或从星期一开始的几周减去6的天数:
/**
** @param {Date} date - date to get start of week for
** @param {Boolean} mondayStart - falsey for Sunday as first day of week,
** truthy for Monday as first day of week
** @returns {Date} - date for first day of week
*/
function getStartOfWeek(date, mondayStart) {
// copy date
var d = new Date(+date);
// days to previous Sunday
var shift = d.getDay();
// Adjust shift if week starts on Monday
if (mondayStart) {
shift = shift? shift - 1 : 6;
}
// Shift to start of week
d.setDate(d.getDate() - shift);
return d;
}
如果您希望减法函数起作用,请说出您要执行的操作。
作为奖励功能,这是一个减数月功能,可以按我认为的预期方式工作:
/*
** @param {number} numberOfMonths - number of months to add to date
** @param {Date} date - date to subract months from
** @returns {Date} - the passed in date object with the specified number of months subtracted
*/
function subtractMonth (numberOfMonths, date) {
// If date not supplied, default to today
var d = date || new Date();
// Get the date
var day = d.getDate();
// Subtract the months
d.setMonth(d.getMonth() - numberOfMonths);
// If date has changed, was too big for month and has rolled over
// into next month, so set to last day of previous month
if (day != d.getDate) d.setDate(0);
return d;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.