473,769 Members | 6,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 31822
charliewest <ch*********@di scussions.micro soft.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.co m>
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.Tota lDays/365.25).ToStrin g() will give you the years diffrence

regards
Ansil

"charliewes t" 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*********@di scussions.micro soft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
charliewest <ch*********@di scussions.micro soft.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.co m>
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.Tota lDays/365.25).ToStrin g() will give you the years diffrence

regards
Ansil

"charliewes t" 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.Pa rse("4/26/1973"));
public static int Age(DateTime birthDate)
{
int years, months, days;
DateDiff(DateTi me.Now, birthDate, out years, out months, out days);
return years;
}
public static void DateDiff(DateTi me 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.ToShortDateS tring(),d2.ToSh ortDateString() ,years,months,d ays));
}
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 ArgumentExcepti on("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
2282
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
742
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: ---------------------------------------------------- Start Date End Date 01/01/2004 12:50pm 02/01/2004 18:40pm 02/01/2004 13:40pm 02/01/2004 13:57pm 02/01/2004 14:30pm 02/01/2004 19:50pm
0
6602
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 there a >datediff function? >
7
15505
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 DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
2
2353
by: TomislaW | last post by:
how can I calculate time needed for execution of my methods in asp.net app?
6
14452
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 letter. Can I use javascripting to create a webpage to allow a user to enter a letter and then click a button to find a future calendar date? I'm just not sure how much user interaction scripting allows. Does java scripting allow buttons, textfields...
2
6166
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 the file. The Sub where the transfer actually takes place is called asynchronously. The (psuedo) code in the Sub goes something like this: While byteCount < filesize bytesRead = Bytes read from NetworkStream
5
13671
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: http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=11469&objectType=File I kluged up this routine that works: // convert date time into that funny matlab serial date time that starts at jan 1, 0000 private string DT2Matlab(DateTime thisDT)
2
1952
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 weekend days
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7409
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.