Ad

Calculate Past Dates From Today In JavaScript

- 1 answer

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

Ad

Answer

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

        });

Ad
source: stackoverflow.com
Ad