admin管理员组文章数量:1279187
I have two input fields in which we enter Month-year. And display data in graph representation. Field1:Jan-2016 Field2:Jan-2015
I have to pare and this fields and if Field1>Field2 then alert message has to display.
How to do the parison in java script
Input type format is Month-year
I have two input fields in which we enter Month-year. And display data in graph representation. Field1:Jan-2016 Field2:Jan-2015
I have to pare and this fields and if Field1>Field2 then alert message has to display.
How to do the parison in java script
Input type format is Month-year
Share Improve this question asked Apr 3, 2016 at 10:00 user4336860user4336860 4- Can you share executable demo/snippet or JSFiddle ? – Rayon Commented Apr 3, 2016 at 10:01
-
Can pare month using
index
ofmonths array
..And year usingNumber parison
– Rayon Commented Apr 3, 2016 at 10:02 - Possible duplicate of Compare two dates with JavaScript – Maen Commented Apr 3, 2016 at 10:03
- Could you please share the fiddle with example. I couldn't make itthis format. – user4336860 Commented Apr 3, 2016 at 10:04
6 Answers
Reset to default 5You can use this small sample in your code
var date_1 = new Date('2017', '01');
var date_2 = new Date('2015', '02');
if (date_1.getTime() > date_2.getTime()) {
/* YOUR CODE */
}
Update: 2016/04/21
With +
operator (more laconic):
if (+new Date('2017', '01') > +new Date('2015', '02')) {
/* YOUR CODE */
}
By using valueOf()
:
if (new Date('2017', '01').valueOf() > new Date('2015', '02').valueOf()) {
/* YOUR CODE */
}
You can separate your strings and have a lookup for the month number and return the wanted parison.
function greater(f1, f2) {
function getDate(f) {
var d = f.split('-');
d[0] = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12 }[d[0].toLowerCase()] || 0;
return d;
}
var d1 = getDate(f1),
d2 = getDate(f2);
return d1[1] > d2[1] || d1[1] === d2[1] && d1[0] > d2[0];
}
document.write(greater('Jan-2016', 'Jan-2015') + '<br>');
document.write(greater('Feb-2016', 'Jan-2016') + '<br>');
document.write(greater('Feb-2014', 'Jan-2016') + '<br>');
If you have the consistent string format of "YYYY-MM"
, e.g. "2020-10"
, "2021-01"
, you don't even have to convert them to date for accurate parison, just pare the string value directly if you just want to know which month is the later month.
Try this:
var d1 = "2020-12";
var d2 = "2021-01";
var greaterMth = d2 > d1 ? d2 : d1;
alert("Greater month is: " + greaterMth);
But for your case, that will be:
var d1 = new Date("Jan-2016");
var d2 = new Date("Jan-2015");
greaterMth = d2 > d1 ? "Jan-2015" : "Jan-2016";
alert("Greater month is: " + greaterMth);
You don't even have to bother about the 1st of the month generated by the new Date()
.
You can use a JavaScript Date object to pare your dates:
new Date("Jan-2016") > new Date("Jan-2015") //-> true
Ideally you should pare Date objects, but Date can't parse strings of the format you use.
If you format the date as "2015-01" and "2016-01" you can use Javascript's Date objects to pare them.
This gives you the benefit that you can continue to use these as actual date representations in your script, should you need to.
Note: if you format the date as, ex, "201501" and "201601" you can use simple greater-than or less-than parisons instead.
So you need to:
- Split the date string up into month and year
- Convert the month string to an integer
- Make a new variable to contain the formatted date
- Compare the formatted dates
You can use a JavaScript library moment.js.
To get the date object, use
var date1= moment("Jan-2016","MMM-YYYY");
Similarly do it for second date and then just pare them as date1 > date2
.
本文标签: Month year comparison in javascriptStack Overflow
版权声明:本文标题:Month year comparison in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741223818a2361481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论