0

The JavaScript object below has some past date value set. I need to calculate these values from the current date using JavaScript.

Right now the date values are manually set but they should be calculated automatically with JavaScript

For example the dates for Last Week should take the start date of the current week and use the day before that as the end date for Last Week and then count 6 days before that to get the start date for Last Week.

How can I do this in JS and work in all browsers?

I am fine with using MomentJS is that is useful for this type of thing?

// 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'
    }
}

This calendar image visualizes the start and end dates I would need to get based on named duration value.

For last week I would need to determine what the date was for the start and end of last week from todays date. In this example would be:
start date: 1/4/2016 and end date: 1/10/2016

enter image description here

JasonDavis
  • 48,204
  • 100
  • 318
  • 537

2 Answers2

1

If I'm understanding correctly.. this should do the trick.

$(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);

        });

Edit: Includes first & last day of weeks and months. This can of course be scaled to your liking.

$(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);

        });
hack3rfx
  • 573
  • 3
  • 16
  • Thanks this would be a nice approach for some of them but the harder ones like `last week` would need to get the start date of the previous week, lets say the monday of the previous week and the end date sunday of previous week. In this case instead of counting 7 days back, the previous week could of ended only a day or 2 ago. Could such dates be determined with this? thnaks – JasonDavis Jan 15 '16 at 03:30
  • Thats great, oddly enough while you did that I was working on start and end for `last week` and `last month` so I did the 2 that you didnt so combined I have them all now! thanks – JasonDavis Jan 15 '16 at 04:49
0

It wouldn't be too hard to calculate it manually without a library. Just do something like this:

 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";
};

You can use Date.getDay() to get the start of the week according to local time (returns 0-6), and adapt the above accordingly.

tanenbring
  • 780
  • 4
  • 14
  • This is somewhat the opposite of what I need. This takes the dates and calculates its duration from today where I need to instead take today and determine what the date would be at set duration. SO for `last week` I need the start date and end date of last week – JasonDavis Jan 15 '16 at 03:23