使用JavaScript

时间:2016-01-15 02:40:04

标签: javascript datetime momentjs

下面的JavaScript对象设置了一些过去的日期值。我需要使用JavaScript从当前日期计算这些值。

现在手动设置日期值,但应使用JavaScript自动计算它们

例如Last Week的日期应采用当前周的开始日期,并使用前一天作为end date的{​​{1}},然后在此前6天计算得到Last Week的{​​{1}}。

我如何在JS中执行此操作并在所有浏览器中工作?

我很好用MomentJS对这类事情有用吗?

start date

此日历图像会根据指定的持续时间值显示我需要获取的Last Week// Date Divider Date values for cocomparisoagainstt Tasks created_at DateTime var dateRangeLabels = { 'Today': { 'start': new Date('2015-09-12T00:00:00'), 'end': new Date('2015-09-12T00:00:00'), 'dateFunc': 'inRange' }, 'Yesterday': { 'start': new Date('2015-09-11T00:00:00'), 'end': new Date('2015-09-11T00:00:00'), 'dateFunc': 'inRange' }, 'Earlier this Week': { 'start': new Date('2015-09-06T00:00:00'), 'end': new Date('2015-09-10T00:00:00'), 'dateFunc': 'inRange' }, 'Last Week': { 'start': new Date('2015-08-30T00:00:00'), 'end': new Date('2015-09-05T00:00:00'), 'dateFunc': 'inRange' }, 'A while Ago': { 'start': new Date('2010-08-30T00:00:00'), 'end': new Date('2015-07-31T00:00:00'), 'dateFunc': 'inRange' }, 'Last Month': { 'start': new Date('2015-08-01T00:00:00'), 'end': new Date('2015-08-31T00:00:00'), 'dateFunc': 'inRange' }, 'Earliar in the Month': { 'start': new Date('2015-08-30T00:00:00'), 'end': new Date('2015-09-05T00:00:00'), 'dateFunc': 'inRange' }, 'other': { 'start': new Date('2015-09-13T00:00:00'), 'end': new Date('2999-12-31T00:00:00'), 'dateFunc': 'inRange' } } 日期。

对于start,我需要确定从今天开始的上周endlast week的日期。在这个例子中将是:
startend

enter image description here

2 个答案:

答案 0 :(得分:1)

如果我正确理解......这应该可以解决问题。



$(function () {
            convertDays = function (d) {
                //Convert days into MilliSeconds
                return d * 86400000;
            }

            var today = new Date();
            var dateRangeLabels = {
                Today: {
                    start: today,
                    end: today,
                    dateFunc: 'inRange'
                },
                Yesterday: {
                    start: new Date(today - convertDays(1)),
                    end: today,
                    dateFunc: 'inRange'
                },
                WeekAgo: {
                    start: new Date(today - convertDays(7)),
                    end: today,
                    dateFunc: 'inRange'
                }
            };

            console.log(dateRangeLabels);

        });




编辑:包括第一个&几周和几个月的最后一天。这当然可以根据自己的喜好进行调整。



$(function () {
            convertDays = function (d) {
                //Convert days into MilliSeconds
                return d * 86400000;
            }
            fDayofWeek = function (d) {
                return new Date(d - convertDays(d.getDay()));
            }
            lDayofWeek = function (d) {
                return new Date((d - convertDays(d.getDay())) + convertDays(6));
            }
            fDayofMonth = function (d) {
                return new Date(d.getFullYear(), d.getMonth(), 1);
            }
            lDayofMonth = function (d) {
                return new Date(d.getFullYear(), d.getMonth() + 1, 0);
            }

            var today = new Date();
            var dateRangeLabels = {
                Today: {
                    start: today,
                    end: today,
                    dateFunc: 'inRange'
                },
                Yesterday: {
                    start: new Date(today - convertDays(1)),
                    end: today,
                    dateFunc: 'inRange'
                },
                WeekAgo: {
                    start: new Date(today - convertDays(7)),
                    end: today,
                    dateFunc: 'inRange'
                },
                ThisWeek: {
                    start: fDayofWeek(today), //Sunday
                    end: lDayofWeek(today) //Saturday
                },
                ThisMonth: {
                    start: fDayofMonth(today),
                    end: lDayofMonth(today)
                }
            };

            console.log(dateRangeLabels);

        });




答案 1 :(得分:0)

如果没有库,手动计算它并不会太难。做这样的事情:

 function sq(num) {
   return num * num;
 }
 var times = {
   "min": 60,
   "hour": sq(60),
   "day": (sq(60) * 24),
   "week": (sq(60) * 24 * 7),
   "month": (sq(60) * 24 * 7 * 4),
   "year": (sq(60) * 24 * 7 * 4 * 12)
 };
 return function(date, max) {
   if (!date) return "";
   var d = new Date(date),
     diff = ((Date.now() - d.getTime()) / 1000);
   if (diff < times.min) {
     return "now";
   } else if (diff < times.hour) {
     return Math.floor(diff / times.min) + " min ago";
   } else if (diff < times.day) {
     return Math.floor(diff / times.hour) + " hours ago";
   } else if (diff < times.week) {
     return Math.floor(diff / times.day) + " days ago";
   } else if (diff < times.month) {
     return Math.floor(diff / times.week) + " weeks ago";
   } else if (diff < times.year) {
     return Math.floor(diff / times.month) + " months ago";
   } //else over a year
   return "over a year ago";
};

您可以使用Date.getDay()根据当地时间(returns 0-6)获取一周的开始,并相应地调整上述内容。