Page 1 of 1
Get DOB from age
Posted: Thu Jul 28, 2022 10:35 pm
by Dharmeshdhabu
Dear members,
I have created software for hospital management. I want DOB from age. I created a script and it gives DOB in string form. I want it in date formate. Please help me. Below is my script
Code: Select all
<script>
function test() {
var age = document.getElementById('agetxt').value;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var poonam = yyyy - age;
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
today = dd+'/'+mm+'/'+yyyy;
document.getElementById('dobresultt').value = dd+'-'+mm+'-'+poonam;
}
</script>
Re: Get DOB from age
Posted: Fri Jul 29, 2022 2:45 am
by Michalio
It is partially impossible, because you don't know the day and month, you can for example assume that today is someones birthday:
Code: Select all
function getDOBFromAge(age) {
const date = new Date();
date.setFullYear(date.getFullYear() - age);
return date.toISOString().substring(0, 10);
}
const dob = getDOBFromAge(18)
console.log({dob});
Re: Get DOB from age
Posted: Fri Jul 29, 2022 4:03 am
by Dharmeshdhabu
Michalio wrote: ↑Fri Jul 29, 2022 2:45 am
It is partially impossible, because you don't know the day and month, you can for example assume that today is someones birthday:
Code: Select all
function getDOBFromAge(age) {
const date = new Date();
date.setFullYear(date.getFullYear() - age);
return date.toISOString().substring(0, 10);
}
const dob = getDOBFromAge(18)
console.log({dob});
I could not understand your solution. I need DOB assuming today is his birth day. In my softwere there is text type input box. When for example 20 is entered softwere should return DOB 29-07-2002 into input box which is date type of input box. Below are my input as well code. When I change input type from text to date it donot give me result. Please help me. I tried code you suggested but actually I could not understand it fully. Thanks for your reply.
Code: Select all
<div class="form-group">
<label>Age</label>
<input class="form-control" id="agetxt" onkeyup="test()" name="age" value="<?php echo trim($age); ?>">
</div>
<div class="form-group">
<label>D.O.B.</label>
<script>
function test() {
var age = document.getElementById('agetxt').value;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var poonam = yyyy - age;
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
today = dd+'/'+mm+'/'+yyyy;
document.getElementById('dobresultt').value = dd+'-'+mm+'-'+poonam;
}
function getDOBFromAge(age) {
const date = new Date();
date.setFullYear(date.getFullYear() - age);
return date.toISOString().substring(0, 10);
}
const dob = getDOBFromAge(18)
console.log({dob});
</script>
<input class="form-control" type="text" id="dobresultt" name="dobt" value="<?php echo trim($dobt); ?>"required>
<input class="form-control" type="date" id="dobresult" name="dob" value="<?php echo trim($dob); ?>"required>
Re: Get DOB from age
Posted: Fri Jul 29, 2022 11:03 am
by Michalio
The date input requires the yyyy-mm-dd format so you can use:
Code: Select all
function getDOBFromAge(age) {
const date = new Date();
date.setFullYear(date.getFullYear() - age);
return date.toISOString().substring(0, 10).split('.').join('-');
}
Re: Get DOB from age
Posted: Fri Aug 19, 2022 9:49 pm
by Dharmeshdhabu
I tried below formula multiple ways but not getting result. Is there a way to convert string into date formate because I get calculated date in string form using my formula but cant' put it into date type input. Please guide me. Also I may not be able to place return date in input so please guide me.
Michalio wrote: ↑Fri Jul 29, 2022 11:03 am
The date input requires the yyyy-mm-dd format so you can use:
Code: Select all
function getDOBFromAge(age) {
const date = new Date();
date.setFullYear(date.getFullYear() - age);
return date.toISOString().substring(0, 10).split('.').join('-');
}
Re: Get DOB from age
Posted: Sun Aug 21, 2022 10:42 pm
by Dharmeshdhabu
I got the solution. It was very simple. I just need to put date ad yyyy-mm-dd and it is done. Just wonderful.
Code: Select all
document.getElementById('dobresultt').value = dd+'-'+mm+'-'+poonam; document.getElementById('dobresult').value = poonam+'-'+mm+'-'+dd;