javascript comparing date formats [duplicate] - javascript

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>

Related

Moment check if date difference is greater than X months

So while I use the following code to check if the difference between 2 dates is X days
var selectedDate = moment(value, 'MM/DD/YYYY');
var today = moment();
today.diff(selectedDate, 'days');
I want to check if the difference between the selected date and the current dates is beyond X month (e.g. say 14 months), then I want to alert some error.
Is there an easy way to do that using moment.js ? I want that leap years and all are also considered automatically.
You can use today.diff(selectedDate, 'months');.
Documentation
var selectedDate = moment("2020-04-21").format('YYYYMMDD');
var newdDate = moment("2020-01-22").format('YYYYMMDD');
var today = moment();
var difference = today.diff(selectedDate, 'months');
console.log('Difference', difference);
var diff = today.diff(newdDate, 'months');
console.log('New Date Difference', diff);
var floatingDiff = today.diff(selectedDate, 'months', true);
console.log('Floating', floatingDiff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You can use moment().diff() to calculated diff b/w today & selected date or pass new date to moment if you want difference between any other date.
import moment from "moment";
const selectedDate = "2020-04-27";
const date_temp = moment().diff(selectedDate, 'months');
console.log(date_temp);

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 Date compare

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

match two datetimes in javascript not working [duplicate]

This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Closed 9 years ago.
I have two dates :
date1 = "2013-07-08 12:30:00"
date2 = "2013-07-08 13:30:00"
Now in javascript i want to match these two dates and its they dont match than i want to delete the appointment and if they match than nothing to do.
I tried this code but its not working :
if(date1 == date2)// Event already exists
{
// do nothing
}
else
{
// delete the record.
}
I tried to compare with "new Date(date1) == new Date(date2)" also but its not working either.
There is some problem in my code or date format. can anyone know how to do this and where i am wrong in this code ?
Thanks in advance.
Two different objects are never the same, you have to compare the numbers that make up the unix timestamp:
var date1 = "2013-07-08 12:30:00",
date2 = "2013-07-08 13:30:00";
var d1 = new Date(date1);
var d2 = new Date(date2);
if (d1.getTime() == d2.getTime()) {
}
FIDDLE
it works for me:
var date1 = "2013-07-08 12:30:00";
var date2 = "2013-07-08 12:30:00";
var date3 = "2013-07-08 12:00:00";
console.log(date1 == date2); //true
console.log(date1 == date3); //false
Jsfiddle link
Adeno has a valid answer but will fail if the dates are milliseconds appart (not the case in OP's example if you use a valid date string). To be sure you're comparing dates by minutes or days you can do:
function sameTime(dt1,dt2){
//round the dates to the minutes
var t1=new Date(dt1);
var t2=new Date(dt2);
t1.setSeconds(0,0);
t2.setSeconds(0,0);
return t1.toString()===t2.toString();
}
function sameDay(dt1,dt2){
//round the dates to the day
var t1=new Date(dt1);
var t2=new Date(dt2);
t1.setHours(0,0,0,0);
t2.setHours(0,0,0,0);
return t1.toString()===t2.toString();
}
function sameMonth(dt1,dt2){
//round the dates to the month
var t1=new Date(dt1);
var t2=new Date(dt2);
t1.setHours(0,0,0,0);
t2.setHours(0,0,0,0);
t1.setDate(1);
t2.setDate(1);
return t1.toString()===t2.toString();
}
var date1 = "2013-07-08T12:30:00",
date2 = "2013-07-08T13:30:00";
var d1 = new Date(date1);
var d2 = new Date(date2);
console.log(sameTime(d1,d2));//precise to the minute
console.log(sameDay(d1,d2));//precise to the day
console.log(sameMonth(d1,d2));//precise to the month

How to get three month ago date in javascript in the format as YYYYDDMM efficiently

I know a way to do in java:
Calendar c5 = Calendar.getInstance();
c5.add(Calendar.MONTH, -6);
c5.getTime(); //It will give YYYYMMDD format three months ago.
Is there a way to do this in javascript. I know that I can use
Date d = new Date(); parse it and do some code to get the format.
But now I dont want to do parsing and getting three month ago date.
var dt = new Date('13 June 2013');
dt.setMonth(dt.getMonth()-1)
Then you can use this piece of code from this answer to convert it to YYYYMMDD
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); // padding
};
d = new Date();
d.yyyymmdd();
Something to be careful of. If you're at Mar 31 and subtract a month, what happens? You can't get Feb 31! See this answer for more details.

Categories