本文介绍了检查一个日期是否在两个日期之间(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一件事,
我需要检查一个日期,这是一个字符串在fomat dd / mm / yyyy
包含在两个日期之间,格式相同(dd / mm / yyyy )

I need one thing, I need check if one date, that is a string in fomat dd/mm/yyyy is contained between two dates, with the same format (dd/mm/yyyy)

我尝试过这个,但不起作用:

I tried this, but doesn't work:

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "02/07/2013";

var from = Date.parse(dateFrom);
var to   = Date.parse(dateTo);
var check = Date.parse(dateCheck );

if((check <= to && check >= from))      alert("date contained");

我使用调试器和检查,变量和变量都有nNN值
可以帮助我吗, 请?
最好的问候,丹尼尔

I used debugger and check,to and from variables have isNaN valueCould you help me, please?Best regards, Daniel

推荐答案

Date.parse 支持格式mm / dd / yyyy不是dd / mm / yyyy。为此,你必须使用一个图书馆,如moment.js,或者做一些如下面给出的内容。

Date.parse supports the format mm/dd/yyyy not dd/mm/yyyy. For that either you have to use a library like moment.js or do something as given below

var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "02/07/2013";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);

console.log(check > from && check < to)

这篇关于检查一个日期是否在两个日期之间(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:32