In this article, you will learn how To Create Age Calculator in asp.net MVC using c# With Example?
In this post, you will How to Create Age Calculator in asp.net MVC using c#.
Complete code here
In .cshtml page
@using usingcsharp;
@{
ViewBag.Title = "Age Calculator | Calculate Your Age Online";
ViewBag.Description = "Determine the time between nowadays and your next birthday mistreatment the date calculator. This is a free online age calculator computes age in terms of years, months, weeks, days, hours, minutes, and seconds, given a date of birth. usingcsharp.com";
}
@*<div class="jumbotron">
<h1></h1>
</div>*@
<div class="row">
<div class="col-md-8">
@*<div class="col-md-9 col-12 gray7 lh-lg">*@
<h1 style="margin-top: 19px;">Age Calculator</h1>
<p class="paragraph font-20 normal-subtitle">
</p>
<article style="margin-bottom:20px;">
<div class="card">
<div class="card-body">
<form>
<div class="form-group">
<label><b>Date of Birth</b></label>
<div class="row">
<div class="col-lg-4">
<select class="form-control" name="ddl_birthdays" id="ddl_birthdays" data-toggle="tooltip" data-placement="top" title="select the Day">
<option value="-1">Select Day</option>
@for (int i = 1; i <= 31; i++)
{
if (DateTime.Now.Day == i)
{
<option value="@i" selected="selected">@i</option>}
else
{
<option value="@i">@i</option>
}
}
</select>
</div>
<div class="col-lg-4">
<select class="form-control" name="ddl_birthmonth" id="ddl_birthmonth">
@foreach (var a in CalcClass.GetMonth())
{
if (DateTime.Now.Month == Convert.ToInt32(a.MonthNo))
{
<option value="@a.MonthNo" selected="selected">@a.Month</option>}
else
{
<option value="@a.MonthNo">@a.Month</option>
}
}
</select>
</div>
<div class="col-lg-4">
<input type="number" autocomplete="off" class="form-control" value="@DateTime.Now.Year" maxlength="4" max="4" name="txt_birthyear" id="txt_birthyear" placeholder="Enter birth year" />
</div>
</div>
</div>
<div class="form-group">
<label><b>Age at the Date of</b></label>
<div class="row">
<div class="col-lg-4">
<select class="form-control" name="ddl_Currentdays" id="ddl_Currentdays">
<option value="-1">Select Day</option>
@for (int i = 1; i <= 31; i++)
{
if (DateTime.Now.Day == i)
{
<option value="@i" selected="selected">@i</option>}
else
{
<option value="@i">@i</option>
}
}
</select>
</div>
<div class="col-lg-4">
<select class="form-control" name="ddl_currentmonth" id="ddl_currentmonth">
@foreach (var a in CalcClass.GetMonth())
{
if (DateTime.Now.Month == Convert.ToInt32(a.MonthNo))
{
<option value="@a.MonthNo" selected="selected">@a.Month</option>}
else
{
<option value="@a.MonthNo">@a.Month</option>
}
}
</select>
</div>
<div class="col-lg-4">
<input type="number" autocomplete="off" class="form-control" value="@DateTime.Now.Year" maxlength="4" max="4" name="txt_currentyear" id="txt_currentyear" placeholder="Enter birth year" />
</div>
<div class="col-lg-12 text-center">
<div class="form-group">
<br />
<input type="button" name="btn_ageCalculator" id="btn_ageCalculator" value="Calculate Age" class="btn btn-primary" />
</div>
</div>
</div>
</div>
</form>
</div>
<div class="card-footer">
<div>
<table class="table table-bordered table-striped" width="100%" style="display:none;" id="tbl_BD_details">
<tr class="bg-primary text-light text-center"><th colspan="2"><h3>Calculated Age Results</h3></th></tr>
<tr><td><strong>Age in Years: </strong></td><td><span id="YMD"> </span></td></tr>
<tr><td><strong>Age in Months: </strong></td><td><span id="tMonthD"></span></td></tr>
<tr><td><strong>Age in Weeks: </strong></td><td><span id="tWeeks"></span></td></tr>
<tr><td><strong>Age in Days: </strong></td><td><span id="tdays"></span></td></tr>
<tr><td><strong>Age in Hours: </strong></td><td><span id="tHours"></span></td></tr>
<tr><td><strong>Age in Minutes: </strong></td><td><span id="tmins"></span></td></tr>
<tr><td><strong>Age in Seconds: </strong></td><td><span id="tsecs"></span></td></tr>
<tr>
<td><strong>Upcoming Birthday: </strong></td>
<td>
<b>
<span id="nxtbday" class="text-danger"></span> left for your next birthday
</b> <img src="/images/birth-cake.svg" alt="birthday cake age calculator" height="30" />
</td>
</tr>
</table>
<div class="row">
<div class="card">
<div class="card-body">
<h2>How can I calculate my birthday?</h2>
<p><b>So now, the instructions bellow:</b></p>
<ul>
<li>1. Enter the number 7.</li>
<li>2. Multiply by the month of your birth (Eg, if your birthday was 29 February, you'd multiply by 2)</li>
<li>3. Subtract 1.</li>
<li>4. Multiply by 13.</li>
<li>5. Add the day of your birth</li>
<li>6. Add 3</li>
<li>7. Multiply by 11</li>
<li>8. Subtract the month of your birth</li>
<li>9. Subtract the day of your birth</li>
<li>10. Divide by 10</li>
<li>11. Add 11</li>
<li>12. Divide by 100.</li>
</ul>
<h2>How do you calculate age in months and days?</h2>
<p>
If you would like to understand the precise age, i.e. how many years, months and days there are between someone's birth date and the current date, write 3 different DATEDIF functions: To get the number of years: =DATEDIF(B2, TODAY(),
"Y") to induce the amount of months: =DATEDIF(B2, TODAY(), "YM").
</p>
<h2>What's the most common birthday UK?</h2>
<p>
More babies ar born in late Sept and early October than at the other purpose of the year.
According to new analysis by the Office for National Statistics (ONS) the most common birthday in England and Wales was 26 September - nearly nine months after Christmas Day.
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</article>
@*</div>*@
</div>
</div>
</div>
<script>
$('#btn_ageCalculator').click(function () {
//var dob = $('#DOB').val();
var birth_day = $('#ddl_birthdays').val();
var birth_month = $('#ddl_birthmonth').val();
var birth_year = $('#txt_birthyear').val();
var current_day = $('#ddl_Currentdays').val();
var current_month = $('#ddl_currentmonth').val();
var current_year = $('#txt_currentyear').val();
$('#tbl_BD_details').show();
//alert(dob);
$.post('/Home/AgeDetail', { birth_day: birth_day, birth_month: birth_month, birth_year: birth_year, current_day: current_day, current_month: current_month, current_year: current_year }, function (data) {
$('#YMD').html(data.Years + ' Years, ' + data.months + ' Months, ' + data.days + ' Days')
$('#tMonthD').html(data.totalmonth + ' Months, ' + data.days + ' Days')
$('#tWeeks').html(data.totalweeks + ' Weeks, ' + data.remainingdays + ' Days')
$('#tdays').html(data.totaldays + ' Days')
$('#tHours').html(data.TotalHours + ' Hours')
$('#tmins').html(data.TotalMinutes + ' Minutes')
$('#tsecs').html(data.TotalSeconds + ' Seconds')
$('#nxtbday').html(data.upcomingbirthday + ' Days')
});
});
</script>
[Route("age-calculator")]
public ActionResult AgeCalculator()
{
return View();
}
public ActionResult AgeDetail(string birth_day, string birth_month, string birth_year, string current_day, string current_month, string current_year)
{
//DateTime dob1 =Convert.ToDateTime( "1994-12-23 00:00:00");
//DateTime tillDate= Convert.ToDateTime( "2019-07-23 00:00:00");
DateTime dob = new DateTime(Convert.ToInt32(birth_year), Convert.ToInt32(birth_month), Convert.ToInt32(birth_day), 00, 00, 00);
DateTime currentDate = new DateTime(Convert.ToInt32(current_year), Convert.ToInt32(current_month), Convert.ToInt32(current_day), 00, 00, 00);
int currentyear = currentDate.Year;
int currentmonth = currentDate.Month;
int currentday = currentDate.Day;
int Bday = dob.Day;
int bmonth = dob.Month;
int byear = dob.Year;
int cday = currentDate.Day;
int cmonth = currentDate.Month;
int cyear = currentDate.Year;
int Ydays = 0;
int Ymonth = 0;
int Yyear = 0;
if (cday >= Bday)
{
Ydays = cday - Bday;
}
else if (cday < Bday)
{
int lastmonthdays = DateTime.DaysInMonth(cyear, cmonth - 1);
Ydays = (cday + lastmonthdays) - Bday;
}
if (cmonth >= bmonth)
{
Ymonth = cmonth - bmonth;
}
else if (cmonth < bmonth)
{
cmonth = (cmonth) + 12;
Ymonth = cmonth - bmonth;
cyear = cyear - 1;
}
if (cyear >= byear)
{
Yyear = cyear - byear;
}
// DateTime newcdate = new DateTime(currentyear, currentmonth, currentday, 00, 00, 00);
TimeSpan difference = currentDate.Subtract(dob);
DateTime age = DateTime.MinValue + difference;
int ageInYears = Yyear;
int ageInMonths = Ymonth;
int ageInDays = Ydays;
long totaldays = Convert.ToInt64(difference.Days);
long remainingdays = Convert.ToInt64(difference.Days % 7);
long totalweeks = Convert.ToInt64(difference.Days / 7);
long thours = Convert.ToInt64(difference.TotalHours);
long tmins = Convert.ToInt64(difference.TotalMinutes);
long tsecs = Convert.ToInt64(difference.TotalSeconds);
long tdays = Convert.ToInt64(totaldays);
long tweeks = Convert.ToInt64(totalweeks);
long remdays = Convert.ToInt64(remainingdays);
//-------------- Upcoming Birthday---------------//
DateTime upcomingbday;
TimeSpan nextbday_days;
if (cmonth > bmonth)
{
upcomingbday = new DateTime(cyear + 1, bmonth, Bday, 00, 00, 00);
nextbday_days = upcomingbday.Subtract(currentDate);
}
else
{
upcomingbday = new DateTime(cyear, bmonth, Bday, 00, 00, 00);
nextbday_days = upcomingbday.Subtract(currentDate);
}
List<ageCalculator> agedetails = new List<ageCalculator>();
agedetails.Add(new ageCalculator
{
Years = Convert.ToString(ageInYears),
months = Convert.ToString(ageInMonths),
days = Convert.ToString(ageInDays),
totalmonth = Convert.ToString(((ageInYears * 12) + ageInMonths)),
//totalweeks=(ageInYears/52)+(ageInMonths/7),
TotalHours = thours.ToString(),
TotalMinutes = tmins.ToString(),
TotalSeconds = tsecs.ToString(),
totaldays = tdays.ToString(),
totalweeks = tweeks.ToString(),
remainingdays = remdays.ToString(),
upcomingbirthday = Convert.ToInt64(nextbday_days.Days).ToString()
});
return Json(agedetails.SingleOrDefault(), JsonRequestBehavior.AllowGet);
}
Enjoy
How To Create Age Calculator in asp.net MVC using c#, Age Calculator With Example?
public static byte[] CreateHTMLtoPDF(string HtmlString)
{
var htmlContent = String.Format(HtmlString);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.Orientation = NReco.PdfGenerator.PageOrientation.Landscape;
//var Orientation = new NReco.PdfGenerator.HtmlToPdfConverter().Orientation=NReco.PdfGenerator.PageOrientation.Landscape;
htmlToPdf.Size = NReco.PdfGenerator.PageSize.A4;
//htmlToPdf.Zoom = -0.55f;
var pageMargin = new NReco.PdfGenerator.HtmlToPdfConverter().Margins;
//htmlToPdf.PageHeight= 210;
//htmlToPdf.PageWidth = 297;
pageMargin.Left = 25;
pageMargin.Right = 25;
pageMargin.Bottom = 25;
pageMargin.Top = 25;
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
byte[] bytes;
using (MemoryStream input = new MemoryStream(pdfBytes))
{
using (MemoryStream output = new MemoryStream())
{
string userpassword = "123";//Change it with User DOB Format dd-MM-yyyy
string Ownerpassword = "123";//Change it with whatever owner is wanted
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, userpassword, Ownerpassword, PdfWriter.ALLOW_SCREENREADERS);
bytes = output.ToArray();
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.BinaryWrite(bytes);
//Response.End();
}
}
return bytes;
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendOTP.aspx.cs" Inherits="Shoping_Cart.SendOTP" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to Send and Verify OTP On Mobile No Using C# In Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Mobile No:
<asp:TextBox ID="txtMobileNo" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send OTP" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Shoping_Cart
{
public partial class SendOTP : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
Random r = new Random();
string OTP = r.Next(1000, 9999).ToString();
//Send message
string Username = "youremail@domain.com";
string APIKey = "YourHash";
string SenderName = "MyName";
string Number = "981111111";
string Message = "OTP code is - " + OTP;
string URL = "http://api.minifycode.in/send/?username=" + Username + "&hash=" + APIKey + "&sender=" + SenderName + "&numbers=" + Number + "&message=" + Message;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
Session["OTP"] = OTP;
//Redirect for varification
Response.Redirect("VerifyOTP.aspx");
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Verify.aspx.cs" Inherits="Cart.VerifyOTP" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Verify OTP</title>
</head>
<body>
<form id="form1" runat="server">
<div>
OTP:
<asp:TextBox ID="txtOTP" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Verify OTP" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Cart
{
public partial class OTP : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["OTP"].ToString() == txtOTP.Text)
{
lblMessage.Text = "You have enter correct OTP.";
Session["OTP"] = null;
}
else
{
lblMessage.Text = "Pleae enter correct OTP.";
}
}
}
}