Algorithm to group messages by time

I need to sort messages in the following order: Received Today , Yesterday , and This Week

Rules for This Week : starts at the most recent Monday - not including Today or Yesterday

Meaning:

If Today is Sunday, Yesterday is Saturday, This Week is Friday, Thursday, Wednesday, Tuesday and Monday

If Today is Thursday, Yesterday is Wednesday, This Week is Tuesday and Monday

But if Today is Tuesday, Yesterday is Monday -- This Week has no days

And if Today is Monday, Yesterday is Sunday -- This Week has no days

I have been struggling figuring out how to solve This Week . I keep getting bugs and there are so many edge cases to consider as well.

Here's my attempt:

var currentDate = new Date()
var currentMonth = currentDate.getMonth()+1
var currentYear = currentDate.getFullYear()
var currentDayofMonth = currentDate.getDate()
var currentDayofWeek = currentDate.getDay()
messages.forEach((message, index) => {
   var date = message.date.substring(0, 10).replace(/-/g, '/')
   var messageDate = new Date(date)
   var messageMonth = messageDate.getMonth()+1;
   var messageYear = messageDate.getFullYear();
   var messageDayOfMonth = messageDate.getDate();
   var messageDayOfWeek = messageDate.getDay();

   //TODAY
   if(currentDayofMonth == messageDayOfMonth && currentMonth == messageMonth && currentYear == messageYear) {
     messageTypeList["Today"].push(message)
     return
   }

   //YESTERDAY
   if(currentDayofMonth == messageDayOfMonth+1 && currentMonth == messageMonth && currentYear == messageYear) {
     messageTypeList["Yesterday"].push(message)
     return
   }

    //THIS WEEK
    if(currentDayofWeek != 1 && currentDayofWeek != 2 && currentMonth == messageMonth && currentYear == messageYear) {
      messageTypeList["This Week"].push(message)
      return
    }
})

Any ideas?

Note

I believe I have an edge case bug in Yesterday -- For example If Today is May 1, it will not count April 30 as Yesterday because the month is different


You could prepare the ground better by preparing some dates related to today's date:

  • Truncate the current date to midnight
  • Get the date of yesterday
  • Get the date of last Monday
  • Then compare the message date with each of these 3 dates: the first one that comes on or before the message date determines the category of the message. If all three are greater than the message date, nothing needs to happen.

    Here is the suggested code:

    // This sample uses a fixed date so the categories can be illustrated better:
    var currentDate = new Date('2017/04/27') // Or leave out the argument for actual date
    currentDate.setHours(0,0,0,0); // set to midnight
    // Prepare all related dates: yesterday and last Monday
    var keys = [];
    keys.push(['Today', new Date(currentDate)]); // clone
    currentDate.setDate(currentDate.getDate() - 1);
    keys.push(['Yesterday', new Date(currentDate)]); // clone
    currentDate.setDate(currentDate.getDate() - (currentDate.getDay()+6)%7);
    keys.push(['This week', new Date(currentDate)]); // clone
    
    var messages = [
        { date: '2017-04-21' },
        { date: '2017-04-22' },
        { date: '2017-04-23' },
        { date: '2017-04-24' }, // Monday
        { date: '2017-04-25' },
        { date: '2017-04-26' },
        { date: '2017-04-27' },
    ];
    
    var messageTypeList = {
        Today: [],
        Yesterday: [],
        "This week": []
    };
    
    messages.forEach( message => {
        var date = message.date.substring(0, 10).replace(/-/g, '/');
        var messageDate = new Date(date);
        var [key] = keys.find( ([key, date]) => messageDate >= date ) || [];
        if (key) messageTypeList[key].push(message);
    });
    
    console.log(messageTypeList);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    first: seperate your code to functions. it will be a lot easier to test and debug. second, add 'else', and in thisWeek() calculate the last monday (as date) and check if the current date is bigger than this date (if the date was after the last monday) - example messageDate.getTime() >= lastMondayDate.getTime()

    The only thing to do is to calculate the last Monday

    pseudo:

    if (isToday(message, currentDate)){
     messageTypeList["Today"].push(message)
    } else if (isYesterday(message, currentDate)){
     messageTypeList["Yesterday"].push(message)
    } else if (isThisWeek(message, currentDate)) {
          messageTypeList["This Week"].push(message)
    }
    
    链接地址: http://www.djcxy.com/p/54892.html

    上一篇: 在DayOfWeek列表中查找最近一周的日期

    下一篇: 按时间分组消息的算法