简体   繁体   English

检查输入日期是否在给定范围内

[英]Check if input date is in given range

I have a form with a simple input type. 我有一个带有简单输入类型的表单。 I'm trying to define a function to check if the date is no more than 6 months older compared to date provided by the input type. 我正在尝试定义一个函数来检查日期是否比输入类型提供的日期早了6个月。 I know i have to convert the String provided by the input in a Date object to make this comparison and then work on get methods of Date object, but i can't figure out how to do this. 我知道我必须转换Date对象中输入所提供的String才能进行此比较,然后对Date对象的get方法进行工作,但是我不知道该怎么做。

Current code: 当前代码:

$scope.compareDates = function(d1) {
        d1 = new Date(d1); //convert String into date Object                
        var d = new Date(); // today date           
        d.setYear(d.getFullYear());
        d.setMonth(d.getMonth() - 6);           

        if(d1 > d ) {
            console.log("ok");
        } else {
            console.log("error");               
        }
    }   

EDIT: 编辑:

I'm sorry, i forgot to add my input. 抱歉,我忘了添加我的输入。 Here it is: 这里是:

<input class="form-control" type="text" placeholder="gg/mm/aaaa" ng-model="sStartDate" ng-change="change()">

Angular Controller: 角度控制器:

$scope.sStartDate = '';

$scope.change = function(){

    var startDt = $scope.sStartDate;
    $scope.compareDates(startDt);

}

If I am reading your code correctly, your date format is days/month/year which is not valid format. 如果我正确阅读了您的代码,则您的日期格式为天/月/年,这是无效的格式。 You need to swap the month and days. 您需要交换月份和日期。

var parts = d1.split(),
    dateStr = parts[1] + "/" + parts[0] + "/" parts[2],
    d1 = new Date(d1),
    d = new Date();
d.setMonth(d.getMonth() - 6);
if(d1 > d ) {
    console.log("ok");
} else {
    console.log("error");               
}

What's about using d.getTime() ? 使用d.getTime()什么用?

$scope.compareDates = function(d1){
        d1 = new Date(d1); //convert String into date Object                
        var d = new Date(); // today date           
        d.setMonth(d.getMonth() - 6);           

        if(d1.getTime() > d.getTime() ) {
            console.log("ok");
        } else {
            console.log("error");                
        }
    }   

Regards. 问候。

I used momentjs for this in our app. 我在我们的应用程序中为此使用了momentjs。 It makes this process very easy and smooth. 它使此过程变得非常轻松和顺畅。

// is6MonthsOld will be a boolean value
var is6MonthsOld = moment(d1).isBefore(moment().subtract(6, 'months'));

If you are comparing dates and not time , make sure to reset time in your date object using d1.setHours(0,0,0,0) . 如果要比较dates而不是time ,请确保使用d1.setHours(0,0,0,0)重置日期对象中的时间。

When you do new Date() , output is a datetime object, but when you do new Date(2015,6,19) , only date is assigned and time is set to 00:00:00 . 当您执行new Date() ,输出是datetime对象,但是当您执行new Date(2015,6,19) ,仅分配日期,并且时间设置为00:00:00

Following code depicts the same: 以下代码描述了相同的内容:

 function compareDates(d1,d2){ return +d2 > +d1; } function compareDatesWithHoursReset(d1,d2){ d1.setHours(0,0,0,0); d2.setHours(0,0,0,0); return +d2 > +d1; } function main(){ var d1 = new Date(); var d2 = new Date(2015, 6, 19); d1.setMonth(d1.getMonth() - 6) console.log(d1); console.log(d2); console.log(compareDates(d2,d1)) console.log(compareDatesWithHoursReset(d2,d1)) } main(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM