Click here to Skip to main content
13,904,565 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($" formControlName="ageYears">
<div class="col-sm-3">
<input type="text"  name='months' class="form-control" placeholder="Months" (keyup)="checkMonth($" formControlName="ageMonths">
<div class="col-sm-3"> 
<input type="text"  name='days' class="form-control" placeholder="Days" (keyup)="checkDay($" formControlName="ageDays">
                                                        <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>

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:

    let mon = year.substring(0,(year.length-1));
    if(year.length>0 && year.length<7){

		if(year == 0){



		else if(year < 1 || year>150){
	else if(year.length>4){

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.
Updated 16-Apr-18 9:41am

1 solution

Rate this: bad
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[^]

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

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");

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

For further details, please see: Moment.js | Guides[^]
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
Maciej Los 17-Apr-18 6:35am

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
Web01 | 2.8.190306.1 | Last Updated 16 Apr 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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