Click here to Skip to main content
13,595,908 members
Rate this:
 
Please Sign up or sign in to vote.
<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 16-Apr-18 9:14am
Updated 16-Apr-18 9:41am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
<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:
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[^]
  Permalink  
v2
Comments
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?
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180621.3 | Last Updated 16 Apr 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100