473,396 Members | 1,966 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,396 software developers and data experts.

How to calculate age using TimeSpan w/ .NET CF?

Can someone pls point me to or recommend the easiest way to calculate
someone´s age using the TimeSpan object, in .NET CF?

Isn´t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?

Thanks.
Nov 16 '05 #1
6 31756
charliewest <ch*********@discussions.microsoft.com> wrote:
Can someone pls point me to or recommend the easiest way to calculate
someone?s age using the TimeSpan object, in .NET CF?

Isn?t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?


What's wrong with the normal non-CF way of taking today's date (as a
DateTime) and their birthdate (likewise), then subtracting one from
another to get a TimeSpan?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
hi
DateTime d1=DateTime.Now ;
DateTime d2 = new DateTime(1978,4,5);
TimeSpan difference = d1.Subtract(d2);

difference.TotalDays/365.25).ToString() will give you the years diffrence

regards
Ansil

"charliewest" wrote:
Can someone pls point me to or recommend the easiest way to calculate
someone´s age using the TimeSpan object, in .NET CF?

Isn´t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?

Thanks.

Nov 16 '05 #3
There´s nothing wrong w/ the normal way if you know how ;-). Unfortunately, i
have been pampered by things like DateDiff. Obviously, dividing by
DaysPerYear is an option, however, i did want to check if there are
"built-in" functions that calculate this. Thanks.

"Jon Skeet [C# MVP]" wrote:
charliewest <ch*********@discussions.microsoft.com> wrote:
Can someone pls point me to or recommend the easiest way to calculate
someone?s age using the TimeSpan object, in .NET CF?

Isn?t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?


What's wrong with the normal non-CF way of taking today's date (as a
DateTime) and their birthdate (likewise), then subtracting one from
another to get a TimeSpan?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
charliewest <ch*********@discussions.microsoft.com> wrote:
There?s nothing wrong w/ the normal way if you know how ;-). Unfortunately, i
have been pampered by things like DateDiff. Obviously, dividing by
DaysPerYear is an option, however, i did want to check if there are
"built-in" functions that calculate this. Thanks.


DateTime birthday = ...;

TimeSpan age = DateTime.Today - birthday;

Then look at whatever you're interested in within the "age" value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Hi Ansil - I like your answer but what if you want to report the age in
Years, Months, Days??
G

"Ansil MCAD" wrote:
hi
DateTime d1=DateTime.Now ;
DateTime d2 = new DateTime(1978,4,5);
TimeSpan difference = d1.Subtract(d2);

difference.TotalDays/365.25).ToString() will give you the years diffrence

regards
Ansil

"charliewest" wrote:
Can someone pls point me to or recommend the easiest way to calculate
someone´s age using the TimeSpan object, in .NET CF?

Isn´t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?

Thanks.

Nov 16 '05 #6
gwenda wrote:
Hi Ansil - I like your answer but what if you want to report the age in Years, Months, Days??
G

"Ansil MCAD" wrote:


Here's what I think you want ---
Bob
int age= Age(DateTime.Parse("4/26/1973"));
public static int Age(DateTime birthDate)
{
int years, months, days;
DateDiff(DateTime.Now, birthDate, out years, out months, out days);
return years;
}
public static void DateDiff(DateTime d1, DateTime d2,
out int years, out int months, out int days)
{
// compute & return the difference of two dates,
// returning years, months & days
// d1 should be the larger (newest) of the two dates
//
//
// y m d
// 3/10/2005 <-- 3/10/2005 0 0 0
// 3/10/2005 <-- 3/09/2005 0 0 1
// 3/10/2005 <-- 3/01/2005 0 0 9
// 3/10/2005 <-- 2/28/2005 0 0 10
// 3/10/2005 <-- 2/11/2005 0 0 27
// 3/10/2005 <-- 2/10/2005 0 1 0
// 3/10/2005 <-- 2/09/2005 0 1 1
// 3/10/2005 <-- 7/20/1969 35 7 21

// we want d1 to be the larger (newest) date
// flip if we need to

if (d1 < d2)
{
DateTime d3= d2;
d2= d1;
d1= d3;
}

// compute difference in total months
months= 12 * (d1.Year - d2.Year) + (d1.Month - d2.Month);

// based upon the 'days',
// adjust months & compute actual days difference
if (d1.Day < d2.Day)
{
months--;
days= GetDaysInMonth(d2.Year, d2.Month) - d2.Day + d1.Day;
}
else
{
days= d1.Day - d2.Day;
}

// compute years & actual months
years= months / 12;
months-= years * 12;

//Debug.WriteLine(string.Format("{0} <-- {1} {2,2} {3,2} {4,2}",
d1.ToShortDateString(),d2.ToShortDateString(),year s,months,days));
}
private static int GetDaysInMonth(int year, int month)
{
// this is also available from Calendar class,
// but just as easy to do ourselves

if (month < 1 || month > 12)
{
throw new ArgumentException("month value must be from 1-12");
}

// 1 2 3 4 5 6 7 8 9 10 11 12
int[] days= {0, 31,28,31,30,31,30,31,31,30,31,30,31};

if (((year/400*400) == year) ||
(((year/4*4)==year) && (year%100 != 0)))
{
days[2]= 29;
}

return days[month];
}

Nov 16 '05 #7

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

Similar topics

1
by: mee-shell | last post by:
I am a newbie of python. I just started to look at the wxDateTime in wx and wanted to know how to use the AddTS method to calculate the timespan. Thanks.
28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
0
by: Tu-Thach | last post by:
TimeSpan span = aEnd.Subtract(aStart); System.Console.WriteLine(span.TotalMilliseconds); Tu-Thach >-----Original Message----- >I want to calculate the time it takes to execute a function is...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
2
by: TomislaW | last post by:
how can I calculate time needed for execution of my methods in asp.net app?
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
2
by: mscdex | last post by:
I have a server application that accepts file transfers (utilitzing tcplistener) and was wondering how I would efficiently go about determining the calculate transfer rate while I am transferring...
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
2
by: Mike | last post by:
Is there a way to calculate the total number of weekdays when provided a date? Example: If I have a start date of 7/11/2008 and I want to go out 10 days, I want to get this: 6 weekdays 4...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.