Javascript Date compare - javascript

In Javascript, if I have dates in the following string formats (mm/dd/yyyy)
date1 = "11/16/2015"
date2 = "11/09/2015"
date3 = "10/31/2015"
Can I directly run comparisions on them ?
like
date1 < date2
date2 > date3
is this the correct way to compare them ? Will it automatically consider the dates (like 31st Oct < 3rd Nov)

You cant do this with string format you have to convert your dates to Date() object before comparaison :
var date1_parts= date1.split("/");
date1 = new Date(date1_parts[2], date1_parts[0] - 1, date1_parts[1]);
var date2_parts= date2.split("/");
date2 = new Date(date2_parts[2], date2_parts[0] - 1, date2_parts[1]);
Now you can compare them using < or > signs :
date1 > date2
true
date1 < date2
false
date1 == date2
false
Hope this helps

You should convert to the date type then compare them
var d1 = new Date("11/16/2015");
var d2 = new Date("11/09/2015");
d1>d2 //true
d2>d1 //false

You can convert date into time format and then compare it.
date1 = new Date("11/16/2015").getTime();
date2 = new Date("11/09/2015").getTime();
date3 = new Date("10/31/2015").getTime();
// and then
// date1 < date2;
// date2 > date3;
// example
alert(date1 > date2);
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

Related

How to get the total Hours by Subtracting two dates using moment?

i have to subtract two days and need to get the total hours.
let date1 = moment.tz(new Date(), "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local().format('MM/DD/YYYY h:mm A');
let date2 = moment.tz(titleDate, "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local().format('MM/DD/YYYY h:mm A');
let getHours = date1.diff(date2, 'hours')
i need to get the total hours like this way. any different way to resolve this issue ?
Also you can use,
let date1 = moment.tz(new Date(), "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local();
let date2 = moment.tz(titleDate, "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local();
var x = moment.duration(date1.diff(date2)).asHours();
from docs
The format function returns a string. So, in your code, date1 and date2 are strings, not momentjs datetime values. Remove the call to format().
After formatted, the date1 does not have diff method which is a string. You can remove the format.
let date1 = moment.tz(new Date(), "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local();
let date2 = moment.tz(titleDate, "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local();
let getHours = date1.diff(date2, 'hours');
Or if date1 and date2 are in same timezone:
let date1 = moment();
let date2 = moment(titleDate);
let getHours = date1.diff(date2, 'hours');
Or
date1.diff(date2) / 1000 / 60 / 60;
date1.diff(date2) return milliseconds.
Try this:
var titleDate = new Date(2018, 11, 24, 10, 33, 30);
var originalDate1 = moment.tz(new Date(), "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local();
var date1 = originalDate1.format('MM/DD/YYYY h:mm A');
var originalDate2 = moment.tz(titleDate, "YYYY-MM-DDTHH:mm:ss", "America/Chicago").local();
var date2 = originalDate2.format('MM/DD/YYYY h:mm A');
console.log(date1+"\n"+date2);
var duration = moment.duration(originalDate2.diff(originalDate1));//myMoment.diff(now, 'days', true);
var hoursDuration = duration.asHours();
console.log(hoursDuration);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data-2012-2022.js"></script>

How to get the days difference of two xs.date in MarkLogic?

What is the appropriate function for getting the days (numeric) difference between two xs.date type?
var date1 = new Date();
var date2 = new Date();
date1 = xs.date("2018-03-29");
date2 = xs.date("2018-04-15");
I tried using fn.daysFromDuration function (see sample below), but it is not returning a numeric value which represents the days difference. It is returning a xs.dayTimeDuration:
var dateDiff = new Date();
dateDiff = fn.daysFromDuration(date2 - date1);
You can't use date2 - date1 to calculate the difference between dates and yield a xs.dayTimeDuration in JavaScript, like you can in XQuery.
In an SJS module, use the xs.date.subtract() from the xs.date object, then you can use fn.daysFromDuration() to obtain the number of days as a number:
const date1 = xs.date("2018-03-29");
const date2 = xs.date("2018-04-15");
fn.daysFromDuration(date2.subtract(date1));

javascript comparing date formats [duplicate]

This question already has answers here:
compare string with today's date in JavaScript
(8 answers)
Closed 4 years ago.
I am trying to compare dates with different formats. I see wrong results when I compare the below, How can I converts dates to a standard format to get correct results.
Heres my Fiddle
var date1 = "4/12/2018 9:52:21 PM";
var date2 ="4/12/2018 9:52:51 PM";
var date3 ="2018/04/12 21:54:40";
var dateCondition1 = (date3>date2);
var dateCondition2 = (date2>date1);
alert(dateCondition1); //shows wrong result
alert(dateCondition2); //shows right result
will this be a correct comparison?
var date1 = new Date("4/12/2018 9:52:21 PM");
var date2 =new Date("4/12/2018 9:52:51 PM");
var date3 =new Date("2018/04/12 21:54:40");
var dateCondition1 = (date3>date2);
var dateCondition2 = (date2>date1);
alert(dateCondition1); //shows wrong result
alert(dateCondition2); //shows right result
try with pure javascript:
var date1 = new Date("4/12/2018 9:52:21 PM");
var date2 =new Date("4/12/2018 9:52:51 PM");
var date3 =new Date("2018/04/12 21:54:40");
var dateCondition1 = (date3.getTime() > date2.getTime());
var dateCondition2 = (date2.getTime() > date1.getTime());
alert(dateCondition1);
alert(dateCondition2);
in the first block of code you are comparing strings
"4/12/2018 9:52:21 PM" > "4/12/2018 9:52:21 PM";
in the second block of code the comparison is correct,
Why does a boolean return in the comparison of 2 strings?
"Matt Ball"
Because, as in many programming languages, strings are compared lexicographically.
You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a through z.
ALTERNATIVE
using the library moment js is easier
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
//Currrent Date
var now = moment(),
custom = moment('Mon 03-Jul-2017, 11:00 AM', 'ddd DD-MMM-YYYY, hh:mm A');
document.write("Compare dates=>" + now.isAfter(custom));
</script>
if you want to use the three comparisons you can format the dates as you want
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
var now = moment();
var date1 = moment('4/12/2018 9:52:21 PM','DD/MM/YYYY hh:mm:ss A');
var date2 = moment('4/12/2018 9:52:51 PM','DD/MM/YYYY hh:mm:ss A');
var date3 = moment('2018/04/12 21:54:40','YYYY/DD/MM hh:mm:ss A');
var dateCondition1 = (date3.isAfter(date2));
var dateCondition2 = (date2.isBefore(date1));
alert(dateCondition1);
alert(dateCondition2);
</script>
</html>

Finding max of dates

I have some dates in some different format.
date1 = 2015-05-27T04:51:12.715Z
date2 = 2015-05-27T04:51:12.782Z
date3 = 2015-05-27T04:51:12.865Z
Dont know what this format means specially the last 4 characters.And how would I find the maximum of these dates in Javascript
Just sort the array and get the first item
var date1 = '2015-05-27T04:51:12.715Z';
var date2 = '2015-05-27T04:51:12.782Z';
var date3 = '2015-05-27T04:51:12.865Z';
var array = [date1, date2, date3];
array.sort(function(a,b) {
return new Date(a) < new Date(b);
});
var max = array[0];

why will this date compare not work in javascript?

So i have a small prob..
if i put in js like below :
var date1 = '05/05/2012'
var date2 = 06/06/2014'
if (date2 > date1){
alert('it works');
}
But this does not seem to work? i mean it does not alert ..why?
But the date2 i am actually using like below :
date2 = document.getElementById(date_id).value; // '06/06/2014'
i echoed or alert the date2 to the browser and its perfeclty displaying as '06/06/2014' ..
But the condition does not work? any clues
You are comparing string objects not dates.
Use a Date() Object
var date1 = new Date('05/05/2012');
var date2 = new Date('06/06/2014');
My suggestion:
var date1 = new Date(year1, month1, day1);
var date2 = new Date(year2, month2, day2);
// compare it
date1 < date2 && console.log('date1 < date2');
date1 > date2 && console.log('date1 > date2');
date1.getTime() == date2.getTime() && console.log('date1 == date2');

Categories