Age Calculator Application In React Native

In this Blog we are going to develop a  Application in React-native in  which user enter there date of birth and get their age 

run following command to Create Project

npx react-native init AgeCalculator

now project is create in the folder 

import moment from "moment";

now do some magic  

import React,{ Component }from 'react';
import {View,Text,TouchableOpacity,TextInput,Platform,} from 'react-native';
import moment from "moment";

export default class HomeScreen extends Component {
  constructor(props){
    super(props)
      this.state={
        age: 'Age',
        dateofbirth: ' ',
        calculateAge: ' ',

}

calculateAge(){
var a = moment(new Date)
var b = moment(this.state.dateofbirth)
var years = a.diff(b, 'year');
b.add(years, 'years');
var months = a.diff(b, 'months');
b.add(months, 'months');
var days = a.diff(b, 'days');
var totalage = (years + ' years ' + months + ' months ' + days + ' days');
this.setState({calculateAge: totalage})
}  

render(){

return(

<View style={{flex: 1,backgroundColor: '#f6f6f6',alignItems: 'center'}}> 

<TextInput
                style ={{height: 40, marginLeft: 15,paddingLeft: 10,marginRight: 15,
                borderWidth: 1,borderRadius: 2, marginTop: 15,width: Dimensions.get('window').width-30}}
                value={this.state.dateofbirth}
                keyboardType="number-pad"
                placeholder = "YYYY-MM-DD"
                onChangeText={(text)=> {this.setState({dateofbirth: text});}}
                onSubmitEditing={()=>{this.calculateAge(this.state.dateofbirth);setTimeout(()=>{this.setState({age: this.state.calculateAge})},1000)
              }}     
                    ></TextInput>

 <Text  style =
               {{height: 40, marginLeft: 15,paddingLeft: 10,marginRight: 15,borderWidth: 1,borderRadius: 5,alignSelf: 'center',marginTop: 15,textAlign: 'center',width: Dimensions.get('window').width-30,padding: 10}}> {this.state.dateofbirth}</Text>

</View>

)

}

npx react-native start

npx react-native run-ios

npx react-native run-android

 

Thank you.
  

Comments

Popular posts from this blog

Counter function for adding item in cart and select the item in the list

Simple function to Encryption on Mobile No,email id or any user information in react-native

compound assignment operator , swift basic operator , comparison operator , ternary conditional operator , simplyprogrammingIDEA.