0

I need to know how long the message was written.

window.date = function (o){
    var r = new Date('1970-01-31 23:59:59').getTime();
    var a = new Date().getTime()-new Date(o).getTime();
    /*o=the date of the message: 2017-04-18 17:00:00*/
    if(new Date().getFullYear()>new Date(o).getFullYear()){
        n = 'more than a year ago';
    }else if(new Date().getMonth()>new Date(o).getMonth()){
       if(j=>r){
           n = 'less than a month ago';
        }else{
           n = 'more than a month ago';
        }
     }else{
        n = 'error'
      }

The problems are these: 1)if you write a message on 31/03 you see after a day(01/04): 'more than a month ago'(it's only one day from the date) 2)if you write a message on 17/04 and today(18/04) you try to read you don't read 'less than a month ago', but you read 'error'. Really i try, but in this page i don't read the solution:https://www.w3schools.com/jsref/jsref_obj_date.asp Can you show me a hypothetical solution?

6
  • 2
    Try changing => to >= in the if statement. Commented Apr 18, 2017 at 17:07
  • => is an arrow function developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Apr 18, 2017 at 17:09
  • new Date().getFullYear()>new Date(o).getFullYear() so date is last day of year, opens it up the first day.... so it would be over a year ago with your check. Commented Apr 18, 2017 at 17:11
  • Ty, but isn't this a prblem, in my code i had written '>=', the real problem is this:'if(new Date().getMonth()>new Date(o).getMonth())', if you comment a day ago, you see 'error'. Commented Apr 18, 2017 at 17:19
  • Why not just add/subtract a month from the current date, and compare it to the date you want to check? General idea is If (dateToCheck < today-one month) { //over a month ago } - You can see basic implementation here. Commented Apr 18, 2017 at 17:33

1 Answer 1

1

Just subtract the amount of time you'd like to check from today's date, and compare it to the date you're checking. If the date you're checking is earlier than the result, then it's over "that amount of time" ago.

A function for this might look like the following:

function checkDate(o) {
    var today = new Date();
    var dateToCheck = new Date(o);
    var monthAgo = new Date(); monthAgo.setMonth(today.getMonth() - 1);
    var yearAgo = new Date(); yearAgo.setFullYear(today.getFullYear() - 1);

    if (dateToCheck < yearAgo) {
        return "Over a year ago.";
    } else if (dateToCheck < monthAgo) {
        return "Over a month ago.";
    } else {
        return "Less than a month ago."
    }
}

Here's an example of it in use:

function checkDate(o) {
    var today = new Date();
    var monthAgo = new Date();
    monthAgo.setMonth(today.getMonth() - 1);
    var yearAgo = new Date();
    yearAgo.setFullYear(today.getFullYear() - 1);
    var dateToCheck = new Date(o);

    if (dateToCheck < yearAgo) {
        return "Over a year ago.";
    } else if (dateToCheck < monthAgo) {
        return "Over a month ago.";
    } else {
        return "Less than a month ago."
    }
}

alert(checkDate(new Date(prompt("Enter a date (mm/dd/yyyy)"))));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.