Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> ASP.Net & Web Forms >> Calculate Age in ASP.Net C#
Calculate Age in ASP.Net C#

Some time we face problem related to calculating exact age in year. In this blog I have explained how to calculate exact age in year in ASP.Net with C#.
Views: 1554     Comments: 0
by AVADHESH PATEL on 4/1/2013

Calculate Age in ASP.Net C#

Some time we face problem related to calculating exact age in year. In this blog I have explained how to calculate exact age in year in ASP.Net with C#.

For demonstration, take one TextBox (TextBox1), one Button (Button1) and one Label (Label1) control on your ASPX page. Now generate Button’s Click event (Button1_Click ()) and write below line of code.

 

// Get current date time

DateTime d = DateTime.Now;

// Changed MM/dd/yyyy format to dd/MM/yyyy fromat

string s = d.ToString("dd/MM/yyyy");

// Convert date time in string (s) to DateTime (Todate) data type

DateTime Todate = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("en-IA"));

// Convert entered date string (TextBox1.Text) to DateTime (givenDate) Data Type

DateTime givenDate = DateTime.Parse(TextBox1.Text);

// Count total days

double days = Todate.Subtract(givenDate).Days;

// Convert days to year and display into Label.Text

Label1.Text = Math.Floor(days / 365.24219).ToString();

Note:

1.      If you want to used Global DateTime format then replay below line from given line

DateTime d = DateTime.Now;

To

DateTime d = DateTime.UtcNow;

DateTime.UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone

DateTime.Now gives the date and time as it would appear to someone in your current locale.

2.      Here I have given DateTime culture is “en-IA”. Change culture according your required.

DateTime Todate = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("en-IA"));

Culture Description

en-AU (English Australia): 24/10/2011

en-IA (English India): 24-10-2011

en-ZA (English South Africa): 2011/10/24

en-US (English United States): 10/24/2011

 

 

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by AVADHESH PATELRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 4940
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.