Find out if someone has logged in, in the last 30 days

const user = {
  name: {
    first: 'John',
    last: 'Doe'
  },
  security: {
    password: '12345',
    pin: '1234',
    lastLogin: '2019-01-01'
  },
  username: 'pcakes4life',
  email: 'jdoe@hotmail.com'



function  trueOrFalse(user) {

}

// Use the object data to return true if
 the user has logged in the last 30 days, otherwise
 return false
}```

Haven’t been able to figure this one out. Anyone have an idea of how to solve this? I tried the setDate() getDate() methods combined with + 30 but even if that were to be correct, i think the result would always be true because the 30 days would always add to the current date having it still be within 30 days

You’re on the right track, can you post some actual code?

function level2exercise5(user) {
  // first i wanted to turn the object data into a single variable 'loginDate' for simplicity. 
  let loginDate = user2.security.lastLogin
  // then I created another variable to store the value of what loginDate would be 30 days later
  let datePlus30 = loginDate.setDate(loginDate.getDate() + 30)
  // if loginDate with the stored value of 2019-01-01 is greater than the datePlus30 variable with the stored value of 2019-01-31, return true. Otherwise return false
    if (loginDate >= datePlus30) {
      return true;
    } else {
      return false
    }
}

console.log(level2exercise5(user2))

/* update: this is the real function name on the exercise and the const variable user is user2 instead of user just to clarify */

You’re definitely on the right track.

What type of data is loginDate? Does that type of data have a setDate method? What type of data does have the setDate method?

loginDate holds the value of 2019-01-01 of the key “Last Login” from the first photo of the thread. The setDate method has the current date plus the 30 days. This was the video i watched to use the setDate method. JavaScript add days to date - YouTube
@ 00:33

But loginDate is a string, right? Do strings have a method called setDate?

In the video you posted, is the date parameter a string or something else?

ah ok, yes loginDate is a string. which means the + 30 is irrelevant…Well now I’m definitely lost lol.

You could turn that string into a Date by passing it to the Date constructor, that may be what you want!

const someDateString = '2021-01-01'
const someDate = new Date(someDateString);

console.log(someDateString.getFullYear()) // ERROR
console.log(someDateString.getMonth()) // ERROR
console.log(someDateString.getDate()) // ERROR

console.log(someDate.getFullYear()) // 2021
console.log(someDate.getMonth()) // 0
console.log(someDate.getDate()) // 1
1 Like

super helpful thanks Colin!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.