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');
Related
How can I find the minimum date when the date format is dd-MM-yyyy like below. The output should return the min date from the dates below.
var a = "12-31-2018"
var b = "12-31-2019"
You can create Date objects using those two strings, compare them and return the older one.
function olderDate(dateA, dateB) {
var date1 = new Date(dateA);
var date2 = new Date(dateB);
if (date1 < date2)
return dateA;
else
return dateB;
}
console.log(olderDate("12/31/2018", "12/31/2019"));
We can try using Date.parse to convert the date strings into bona fide dates. Then, compare them:
var a = "12/31/2018";
var b = "12/31/2019";
var dateA = Date.parse(a);
var dateB = Date.parse(b);
var dateMin = dateA < dateB ? a : b;
console.log("minimum date is " + dateMin);
function olderDate(dateA, dateB) {
var date1 = new Date(dateA);
var date2 = new Date(dateB);
if (date1 < date2)
return dateA;
else
return dateB;
}
console.log(olderDate("12/31/2018", "12/31/2019"));
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));
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>
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
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];