473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Retrieving Year and calculating difference

RP
I have a date entered in a TextBox. On this TextBox LostFocus, I want
to retrieve the year part and then calculate the difference from
current year.

Aug 14 '07 #1
4 1738
You do not need to "retrieve the year part". If both are DateTime object, all
you need to do is date math with them and get the result.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"RP" wrote:
I have a date entered in a TextBox. On this TextBox LostFocus, I want
to retrieve the year part and then calculate the difference from
current year.

Aug 14 '07 #2
RP
Did something like this:

--------------------------------------------------------------------
DateTime myDate = txtDOB.Text;
int myYear = myDate.Year;
int PresentYear = DateTime.Now.Year;
txtAge.Text = (PresentYear - myYear);
----------------------------------------------------------------------

Please correct.

Peter Bromberg [ C# MVP ] wrote:
You do not need to "retrieve the year part". If both are DateTime object, all
you need to do is date math with them and get the result.
Aug 14 '07 #3
RP wrote:
Did something like this:

--------------------------------------------------------------------
DateTime myDate = txtDOB.Text;
int myYear = myDate.Year;
int PresentYear = DateTime.Now.Year;
txtAge.Text = (PresentYear - myYear);
----------------------------------------------------------------------

Please correct.
What about the code doesn't do what you want?

The one thing that jumps out to me is that if you are really looking for
a calculation of "age", then the above doesn't take into account the
rest of the date. Is that your concern?

If so, then something like this might work for you:

DateTime myDate = DateTime.Parse(txtDOB.Text),
dateNow = DateTime.Now;
int cyearsAge = dateNow.Year - myDate.Year;

myDate = myDate.AddYears(cyearsAge);
if (myDate dateNow)
{
cyearsAge--;
}

txtAge.Text = cyearsAge.ToString();

Pete
Aug 14 '07 #4
On Tue, 14 Aug 2007 13:28:50 -0700, RP <rp*********@gmail.comwrote:
>Did something like this:

--------------------------------------------------------------------
DateTime myDate = txtDOB.Text;
int myYear = myDate.Year;
int PresentYear = DateTime.Now.Year;
txtAge.Text = (PresentYear - myYear);
----------------------------------------------------------------------

Please correct.

Peter Bromberg [ C# MVP ] wrote:
>You do not need to "retrieve the year part". If both are DateTime object, all
you need to do is date math with them and get the result.
Of course it depends on your definition for a year. How will the code
handle a situation like 1 Jan 2007 and 31 Dec 2007? Personally I'd do
the following:

DateTime myDate = DateTime.Parse(txtDOB.Text);
TimeSpan interval = myDate - DateTime.Now
decimal years = interval.Days / 365

In this case years will have a pretty good representation of elasped
years, which you can then decide to round up depending on the level of
accuracy you want. So 0.999 could be pushed up to 1.

--
http://bytes.thinkersroom.com
Aug 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
14
by: Protoman | last post by:
Hi!!! I need some help on a project I'm that calculates leap years; I'm getting errors that I have no idea what they mean; here's the code: #include <iostream> #include <cstdlib> using...
14
by: Alan | last post by:
Hi everyone! I'm trying to produce a periodic financial report on projects from various departments. My database is set up with the tables tblDepartment, tblProjects, tblPeriods, and tblBudgets...
1
by: Tony Williams | last post by:
I have a table with two fields, txtvalue (a number field) and txtmonth ( a date/time field). I want to create a report that shows the difference in value between the value in txtvalue in one value...
19
by: Ricardo Perez Lopez | last post by:
Hello everyone: I'm a PostgreSQL newbie, working now with dates, times, timestamps and intervals. I have three questions about the above: FIRST: --------
4
by: Dino Buljubasic | last post by:
Hi, I have noticed that when I use Socket.Receive(byte) it retrieves all information about files including day, month but without year. Date for directories is full though. What would be...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
5
by: Kasrav | last post by:
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here def year2(): print'This program validates days and months of the...
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.