I want to check if 08/2014 is greater than 02/2014..
How can I do that in Javascript?? Could someone help me please...
Currently I only check for future year. Here is the code snippet.
this.isDateGreaterThanCurrent=function(b){
return parseInt(b)>parseInt(new Date().getFullYear());
};
I want to check if 08/2014 is greater than 02/2014..
How can I do that in Javascript?? Could someone help me please...
Currently I only check for future year. Here is the code snippet.
this.isDateGreaterThanCurrent=function(b){
return parseInt(b)>parseInt(new Date().getFullYear());
};
Share
Improve this question
asked Feb 6, 2014 at 5:43
AnsumanAnsuman
1,4944 gold badges17 silver badges32 bronze badges
1
-
I prefer moment.js, then ..
moment("08/2014", "MM/YYYY").isBefore(moment("02/2014", "MM/YYYY"))
. However, you're more than free to do this by hand. I would start by creating a function that took in "02/2014" and returns "2014-02"; then that can be pared lexically with a standard string pare. – user2864740 Commented Feb 6, 2014 at 5:50
5 Answers
Reset to default 3Date
objects can be pared with each other. So instead of parsing them you can do it as date
object itself.
function isDateGreaterThanToday(b) {
var dS = b.split("/");
var d1 = new Date(dS[1], (+dS[0] - 1));
var today = new Date();
if (d1 > today) {
return "D is greater";
} else {
return "today is greater";
}
}
console.log(isDateGreaterThanToday("08/2014"))
JSFiddle
With moment.js (which I remend), then ..
var d1 = moment("08/2014", "MM/YYYY")
var d2 = moment("02/2014", "MM/YYYY")
d1.isBefore(d2) // false
d1.isAfter(d2) // true
However, this can be done by hand without much effort.
Create a function that took in "MM/YYYY" and returns "YYYY-MM". The result can then be pared with a standard string pare. The major-first ponent layout is one reason why a format like ISO 8601 is nice - even as a string, it can be easily ordered.
function toPartialIso(str) {
var m = str.match(/(\d{2})\/(\d{4})/);
if (m) {
return m[2] + "-" + m[1]
}
throw "bad date!"
}
toPartialIso("08/2014") < toPartialIso("02/2014") // false
toPartialIso("08/2014") > toPartialIso("02/2014") // true
construct two date object with
new Date(year, month) //month start from 0 (ie january is 0)
as,
d1 = new Date(2014, 7) // 08/2014 (month - 1)
d2 = new Date(2014, 1) // 02/2014
Now check,
Date.parse(d1) > Date.parse(d2)
Date.parse()
, parses a string representation of a date and returns the number of milliseconds since 1 January, 1970, 00:00:00, local time.
You're nearly there ... Using the split method to create an array out of your date
this.isDateGreaterThanCurrent=function(b){
var a = b.split('/');
if(parseInt(a[1])>parseInt(new Date().getFullYear())) return true;
else if (parseInt(a[1]) == parseInt(new Date().getFullYear()) && parseInt(a[0])>parseInt(new Date().getMonth())+1)return true;
else return false;
};
dont forget getMonth returns values from 0 to 11, not from 1 to 12 !
To pare two date which are in your format, we have to parse the dates and extract the month and year and pare them
Its preffered to use some third party library
- datejs
- momentjs
If you want to do this manually the sample code looks like
function checkMonths(month1, month2) {
var m1 = month1.split("/");
var m2 = month2.split("/");
var res = -1;
if( parseInt(m1[1] ) > parseInt(m2[1]) ) {
res = 1;
} else if(parseInt(m1[1] ) == parseInt(m2[1])) {
if(parseInt(m1[0] ) > parseInt(m2[0])) {
res = 1;
} else if(parseInt(m1[0] ) == parseInt(m2[0])) {
res = 0;
}
}
return res;
}
Working demo JSFiddle demo