1

I need to compare two timestamp values one is retrieving from the database and the other is the constant date which is the default value.

 var userdate = doc.data().Subscriptionenddatefrombackend;// the value here I am getting is : 2020-03-02T09:49:05.000Z
 var settingdate = new Date('2019/03/04'); // the value here I am getting is : 2019-03-04T00:00:00.000Z

 if(settingdate < userdate){
    console.log("new user") // the code enters into this loop instead of else loop why?
 }
 else{
    console.log("old user") // should print this line
 }
6
  • 1
    Is Subscriptionenddatefrombackend a string by any chance? Commented Mar 5, 2020 at 11:49
  • So, what's not working there? both dates seem to be actual date objects... Commented Mar 5, 2020 at 11:49
  • @Jamiec - Datatype of " Subscriptionenddatefrombackend" is timestamp. Commented Mar 5, 2020 at 11:51
  • timestamp isnt a datatype in javascript Commented Mar 5, 2020 at 11:55
  • It is the datatype in my backend(Firestore) and please go through the edited question. Commented Mar 5, 2020 at 11:57

3 Answers 3

1

You are doing a comparison operation on an object and a string.

userdate is a string, and settingdate is an object.

You should try creating a new Date object from the userdate string.

let userdate = new Date( doc.data().Subscriptionenddatefrombackend )

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

1 Comment

It's not working
0

You can parse it to create an instance of Date and use the built-in comparators: Convert userdate to a Date object also settingsdate is already a date object.

new Date(userdate) > settingsdate
new Date(userdate) < settingsdate

1 Comment

It is not working
0

by using Date.parse() :

var userdate = doc.data().Subscriptionenddatefrombackend;// the value here I am getting is : 2020-03-02T09:49:05.000Z
var settingdate = new Date('2019/03/04'); // the value here I am getting is : 2019-03-04T00:00:00.000Z

if(settingdate.getTime() < Date.parse(userdate)){
   console.log("new user") // the code enters into this loop instead of else loop why?
}
else{
   console.log("old user") // should print this line
}

4 Comments

No point in parsing settingdate. That's already a Date object.
@Cerbrus date parse return A number representing the milliseconds elapsed since 1970 so we can compare two numbers; I'm not going compare two date object, here we are comparing two number
So use .getTime() for the existing date object.
@Cerbrus yeah, that makes sense; updated();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.