简体   繁体   中英

Calculate past dates from today in JavaScript

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

在此处输入图片说明

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM