Click here to Skip to main content
15,560,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<div class="col-md-12" *ngIf="!DOBKnown">
<div class="col-md-6">
<div class="form-group">
<label class="col-sm-3 control-label mandatory">Approx. Age</label>
<div class="col-sm-3">
<input type="text" name='years' class="form-control" placeholder="Years" maxlength="3" (keyup)="checkYear($event.target.value)" formControlName="ageYears">
</div>
<div class="col-sm-3">
<input type="text"  name='months' class="form-control" placeholder="Months" (keyup)="checkMonth($event.target.value)" formControlName="ageMonths">
</div>
<div class="col-sm-3"> 
<input type="text"  name='days' class="form-control" placeholder="Days" (keyup)="checkDay($event.target.value)" formControlName="ageDays">
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="form-group">
                                                                <label class="col-sm-3 control-label"> Approx. Date Of Birth </label>
                                                                <div class="col-sm-9">
                                                                    <input type="date" class="form-control" (keyup)="calculate()" formControlName="dateOfBirth" [(ngModel)]="dobYear" disabled>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>



createPatient() {
        this.createPatientForm = new FormGroup({
            firstName: new FormControl('', <any>Validators.required),
            lastName: new FormControl('', <any>Validators.required),
            dateOfBirth: new FormControl('', <any>Validators.required),
            ageYears: new FormControl('', <any>Validators.required),
            ageMonths: new FormControl('', <any>Validators.required),
            ageDays: new FormControl('', <any>Validators.required),

        });
    }
**Patient create form**


What I have tried:

checkYear(year){
    let mon = year.substring(0,(year.length-1));
    if(year.length>0 && year.length<7){
		if(year == 0){
            year=mon;
		}
		else if(year < 1 || year>150){
            year='';
		}
	}
	else if(year.length>4){
        year=mon;
	}
}


I tried to check the year.But I dint understand how to calculate month and day.If it is less than the current month and day.How will I give.
Posted
Updated 16-Apr-18 10:41am

1 solution

It's quite obvious...

You have to get current date and susbstract from it days, months and years.
I'd suggest to use Moment.js[^]

HTML:
HTML
<div>
  Years ago: <span id="cntOfYears"></span>
</div>
<div>
  Months ago: <span id="cntOfMonths"></span>
</div>
<div>
  Days ago: <span id="cntOfDays"></span>
</div>
<div>
  Birth date: <span id="birthDate"></span>
</div>


JavaScript:
JavaScript
var cntOfYears = 25;
var cntOfMonths = 10;
var cntOfDays = 5;

var birthDate = moment().subtract(cntOfYears, 'years').subtract(cntOfMonths, 'months').subtract(cntOfDays, 'days');

document.getElementById("cntOfYears").innerHTML = cntOfYears;
document.getElementById("cntOfMonths").innerHTML = cntOfMonths;
document.getElementById("cntOfDays").innerHTML = cntOfDays;
document.getElementById("birthDate").innerHTML = birthDate.format("YYYY-MM-DD");


Result:
Years ago: 25
Months ago: 10
Days ago: 5
Birth date: 1992-06-11


For further details, please see: Moment.js | Guides[^]
 
Share this answer
 
v2
Comments
Member 13782684 17-Apr-18 0:19am    
Thank you so much.
Maciej Los 17-Apr-18 3:02am    
You're very welcome.
Can you accept my answer as a solution (green button) to remove your question from unanswered list?
Member 13782684 17-Apr-18 3:09am    
Sure
Maciej Los 17-Apr-18 6:35am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900