Когда вы оцениваете код, очень ясно, что (==) сравнивается в соответствии с адресом памяти, а equals (Object o) сравнивает hashCode () экземпляров. Вот почему сказано, что не разорвать контракт между equals () и hashCode (), если вы не столкнетесь с неожиданностями позже.
String s1 = new String("Ali");
String s2 = new String("Veli");
String s3 = new String("Ali");
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
System.out.println("(s1==s2):" + (s1 == s2));
System.out.println("(s1==s3):" + (s1 == s3));
System.out.println("s1.equals(s2):" + (s1.equals(s2)));
System.out.println("s1.equal(s3):" + (s1.equals(s3)));
/*Output
96670
3615852
96670
(s1==s2):false
(s1==s3):false
s1.equals(s2):false
s1.equal(s3):true
*/
var now = moment(new Date());
var then = moment("04-09-2013 14:59:40", "DD-MM-YYYY HH:mm:ss");
var timeDifferenceInSeconds = now.diff(then, 'seconds');
var timeDifferenceInHours = now.diff(then, 'hours');
var timeDifferenceInDay = now.diff(then, 'days');
console.log(timeDifferenceInHours, timeDifferenceInSeconds, timeDifferenceInDay);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
в соответствии с документацией момента , в основном без параметров, функция diff
возвращает число в milliseconds
, вы можете использовать years
, months
, weeks
, [ 119], hours
, minutes
и seconds
, если вы хотите использовать другую единицу времени.
принять во внимание:
По умолчанию момент # diff будет обрезать результат до нуля десятичных разрядов, возвращая целое число. Если вам нужно число с плавающей запятой, передайте true в качестве третьего аргумента.
blockquote>проверяет рабочий фрагмент кода, который показывает другие типы блоков.
const getTimeDiff = (differenceIn = 'milliseconds', floating= false) => { var now = moment("04-09-2013 15:00:00", "DD-MM-YYYY HH:mm:ss"); var then = moment("04-09-2013 14:59:40", "DD-MM-YYYY HH:mm:ss"); //this return the difference between now and then in milliseconds as default var timeDifference = now.diff(then, differenceIn, floating); console.log("Time difference - " + timeDifference + ' ' + differenceIn); } getTimeDiff(); getTimeDiff('seconds'); getTimeDiff('seconds', true); getTimeDiff('minutes'); getTimeDiff('minutes', true); getTimeDiff('hours'); getTimeDiff('hours', true); getTimeDiff('days'); getTimeDiff('days', true); getTimeDiff('weeks'); getTimeDiff('weeks', true); getTimeDiff('months'); getTimeDiff('months', true); getTimeDiff('years'); getTimeDiff('years', true);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.js"></script>
В своем коде убедитесь, что вы пытаетесь выполнить
diff
изString
. В основном вы делаетеformat
перед применениемdiff
.
const getTimeDiffWithCurrentTime = (lastLoggedInDateTime) => { //***PROBLEM IS HERE IN THE BELLOW LINE WHERE THE OUTPUT IS 1547571895000*** var momentLastLoggedInDateTime = moment(lastLoggedInDateTime, "DD-MM-YYYY HH:mm:ss"); //var currentDateTime = moment().format("DD-MM-YYYY HH:mm:ss"); IF YOU DO THIS, you get a STRING var currentDateTime = moment() console.log(' lastLoggedInDateTime - ' + lastLoggedInDateTime + 'momentLastLoggedInDateTime - ' + momentLastLoggedInDateTime + ' currentDateTime - ' + currentDateTime); var timeDifference = currentDateTime.diff(momentLastLoggedInDateTime); console.log("Time difference - " + timeDifference); return timeDifference; } const newDate = new Date(new Date() - 10000); //minus 10 seconds getTimeDiffWithCurrentTime(newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.js"></script>