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
 }
Sravya Gajavalli
  • 121
  • 1
  • 10

3 Answers3

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 )

Chris L
  • 129
  • 5
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
joy08
  • 9,004
  • 8
  • 38
  • 73
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
}
Mechanic
  • 5,015
  • 4
  • 15
  • 38