473,401 Members | 2,146 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,401 software developers and data experts.

Date and Time Calculation.

Dear All,

I am upgrading my skills from VB 6.0 to VB.Net. They say you can do all
date and time calculations using the properties and methods of DateTime
structure withour recourse to the functions and properties of the VB
runtime library. (oh yeah !!!)

I have been trying to determine the year interval between two dates using
the properties and methods of the DateTime structure so that it is
consistent with the .Net Framework.

I know how to this using the properties and functions of the VB run time
library that inclues the Microsoft VisualBasic namespace.

E.g. number of years = DateDiff(DateInterval.Year, dtmBirthDate,
dtmTodaysDate)

So the question is how do i get the number of years "accurately" using the
properties and methods of DataTime structure given that

date.subtract(date) = timespan and there seems to be no direct way to
extract the year value from TimeSpan.

date.subtract(timespan) = date , but i dont see how i would get my year
from this.

Can anybody help me with this PRETTY PLEASE

Yard Dancer
Jul 28 '06 #1
4 12570

"YardDancer" <da*********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Dear All,

I am upgrading my skills from VB 6.0 to VB.Net. They say you can do all
date and time calculations using the properties and methods of DateTime
structure withour recourse to the functions and properties of the VB
runtime library. (oh yeah !!!)

I have been trying to determine the year interval between two dates using
the properties and methods of the DateTime structure so that it is
consistent with the .Net Framework.

I know how to this using the properties and functions of the VB run time
library that inclues the Microsoft VisualBasic namespace.

E.g. number of years = DateDiff(DateInterval.Year, dtmBirthDate,
dtmTodaysDate)

So the question is how do i get the number of years "accurately" using the
properties and methods of DataTime structure given that

date.subtract(date) = timespan and there seems to be no direct way to
extract the year value from TimeSpan.

date.subtract(timespan) = date , but i dont see how i would get my year
from this.

Can anybody help me with this PRETTY PLEASE

Yard Dancer
Dim fromDate As DateTime = New DateTime(2000, 3, 15)
Dim toDate As DateTime = DateTime.Now
If you want just the number of years two dates span, you just need to
subtract the two DateTime.Years.

Dim totalYears As Integer = toDate.Year - fromDate.Year

If you want the number of years, calculated by the total number of days...it
can get a little more complex. If you don't care about leap years and
stuff, you could use:

Dim totalYears As Integer = toDate.Subtract(fromDate).Days / 356

HTH,
Mythran
Jul 28 '06 #2
Thanks Mythran for your reply!

I have found out that the best way is

number of years = (((date1.subtract(date2)).Days) / 365.25)

namely timespan.Days / 365.25
Thanks for your effort though

Yard Dancer
lAge = (((dtmTodaysDate.Subtract(dtmBirthDate)).Days) / 365.25)

"Mythran" <ki********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>
"YardDancer" <da*********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Dear All,

I am upgrading my skills from VB 6.0 to VB.Net. They say you can do all
date and time calculations using the properties and methods of DateTime
structure withour recourse to the functions and properties of the VB
runtime library. (oh yeah !!!)

I have been trying to determine the year interval between two dates using
the properties and methods of the DateTime structure so that it is
consistent with the .Net Framework.

I know how to this using the properties and functions of the VB run time
library that inclues the Microsoft VisualBasic namespace.

E.g. number of years = DateDiff(DateInterval.Year, dtmBirthDate,
dtmTodaysDate)

So the question is how do i get the number of years "accurately" using
the properties and methods of DataTime structure given that

date.subtract(date) = timespan and there seems to be no direct way to
extract the year value from TimeSpan.

date.subtract(timespan) = date , but i dont see how i would get my year
from this.

Can anybody help me with this PRETTY PLEASE

Yard Dancer

Dim fromDate As DateTime = New DateTime(2000, 3, 15)
Dim toDate As DateTime = DateTime.Now
If you want just the number of years two dates span, you just need to
subtract the two DateTime.Years.

Dim totalYears As Integer = toDate.Year - fromDate.Year

If you want the number of years, calculated by the total number of
days...it can get a little more complex. If you don't care about leap
years and stuff, you could use:

Dim totalYears As Integer = toDate.Subtract(fromDate).Days / 356

HTH,
Mythran


Jul 28 '06 #3

"YardDancer" <da*********@hotmail.comwrote in message
news:eD**************@TK2MSFTNGP03.phx.gbl...
Thanks Mythran for your reply!

I have found out that the best way is

number of years = (((date1.subtract(date2)).Days) / 365.25)

namely timespan.Days / 365.25
Thanks for your effort though

Yard Dancer
lAge = (((dtmTodaysDate.Subtract(dtmBirthDate)).Days) / 365.25)
Now, if you are trying to find an age of a person, that's
different...slightly...try the following method:

Private Function GetAge(ByVal StartDate As DateTime) As Integer
Dim today As DateTime = DateTime.Today

If today < StartDate
Throw New ArgumentException( _
"The StartDate must be before today's date.", _
"StartDate" _
)
End If

Dim totalYears As Integer = today.Year - StartDate.Year

If today.Month < StartDate.Month OrElse ( _
today.Month = StartDate.Month AndAlso _
today.Day < StartDate.Day _
)
' Offset by a year if it has not been a full year since the last
' birth date.
totalYears -= 1
End If

Return totalYears
End Function

Note, I haven't really tested the logic out but I believe it's right :)
Double check it to make sure...

This calculates a person's (or objects) age, in years, and returns the Years
lapsed, but not including the current year if the date (Month and Day) has
not been reached yet. For example, my irl birthdate is August 10th. The
GetAge method will return 25 years instead of 26 as the method you chose to
use would. I'm 25 right now, not 26. I will be 26 soon, but not quite
there yet :)

Got it?

HTH,
Mythran
Jul 28 '06 #4
Now that is what i call "ACCURATE"

Thanks Mythran

Yard Dancer

"Mythran" <ki********@hotmail.comwrote in message
news:uD****************@TK2MSFTNGP06.phx.gbl...
>
"YardDancer" <da*********@hotmail.comwrote in message
news:eD**************@TK2MSFTNGP03.phx.gbl...
>Thanks Mythran for your reply!

I have found out that the best way is

number of years = (((date1.subtract(date2)).Days) / 365.25)

namely timespan.Days / 365.25
Thanks for your effort though

Yard Dancer
lAge = (((dtmTodaysDate.Subtract(dtmBirthDate)).Days) / 365.25)

Now, if you are trying to find an age of a person, that's
different...slightly...try the following method:

Private Function GetAge(ByVal StartDate As DateTime) As Integer
Dim today As DateTime = DateTime.Today

If today < StartDate
Throw New ArgumentException( _
"The StartDate must be before today's date.", _
"StartDate" _
)
End If

Dim totalYears As Integer = today.Year - StartDate.Year

If today.Month < StartDate.Month OrElse ( _
today.Month = StartDate.Month AndAlso _
today.Day < StartDate.Day _
)
' Offset by a year if it has not been a full year since the last
' birth date.
totalYears -= 1
End If

Return totalYears
End Function

Note, I haven't really tested the logic out but I believe it's right :)
Double check it to make sure...

This calculates a person's (or objects) age, in years, and returns the
Years lapsed, but not including the current year if the date (Month and
Day) has not been reached yet. For example, my irl birthdate is August
10th. The GetAge method will return 25 years instead of 26 as the method
you chose to use would. I'm 25 right now, not 26. I will be 26 soon, but
not quite there yet :)

Got it?

HTH,
Mythran


Jul 29 '06 #5

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

Similar topics

9
by: tertius | last post by:
Hi, Is there a more pythonic way to write or do a date difference calculation? I have as input two date fields in the form 'YYYY-MM-DD' TIA Terius
2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Manny Chohan | last post by:
Hi Can anyone tell me how i can calculate yesterday date using asp? Thanks manny
1
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN...
1
by: mene | last post by:
I have a field that contains date information, and sometimes time information as well. I would like to be able to take that date and do a calculation on it. Here are some examples of what is in...
4
by: qtip | last post by:
I have a simple table the has First Name , Last Name, SSN, Date&Time. I have a report that will show all this information but I would like to put in at calculation to tell the difference between 2...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
1
by: jonny | last post by:
Option Explicit Private mdTarget1 As Date Private mdTarget2 As Date ' called on form load Public Sub SetRecordStartTime() mdTarget1 = DateAdd("n", 60, Time) mdTarget2 = DateAdd("n", 120,...
16
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after...
11
by: Connie via AccessMonster.com | last post by:
Hi Access Building Friends, I am building a database for a manufacturer who needs to know the projected End_Date of each job. I know the Start_Date and the total days required to do the job. ...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
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.