开发者

Compare to dates Javascript? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Compare 2 d开发者_Python百科ates with JavaScript

Hi,

I'm working on form validation for my application and i want to know where i would be to start looking at to find date comparison eg a start date and an end date. The dates inputted in to my application are in the form: DD/MM/YYYY. Thanks in Advance,

Dean


If you are using the Javascript Date object, you can simply do:

var startDate = new Date();
var endDate = getEndDate();

if (endDate < startDate)
    alert("Houston, we've got a problem!");

EDIT: Changed naming a bit just to stick to camelCase convention, even though I despise it.


this function lets you convert dates to timestamps with wich you could work: http://caioariede.com/arquivos/strtotime.js


First, you'll want to parse the text into Date objects, then use the language's built-in date comparison. For example:

var dateStr = document.getElementById('foo').value;
var date = Date.parse(dateStr);

var dateStr2 = document.getElementById('foo2').value;
var date2 = Date.parse(dateStr2);

if (date < date2) {
    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜