C# - A Real Timespan Object With .Years And .Months?

Dec 16, 2009

Consider the following 2 scenarios: Scenario 1). Today is May 1st 2012, and Scenario 2). Today is September 1st 2012.Now, consider that we write on our webpage the following about a comment someone has left: "This comment was written 3 months and 12 days ago". The amount of days in both these scenarios will ALWAYS be different even though the statement is exactly the same. In Scenario 1, "3 months and 12 days" would equal 102 days. However, in Scenario 2, "3 months and 12 days" would be 104 days!Now, to corner in on my point, lets use a different example and say that someone left a comment on our site on Jan 30th 2013, and today is March 10th 2013. Our real TimeSpan object needs to know this relative date, and can figure out the following:

That there is 10 days in March,That there is 1 day in Jan (counting from 30th to 31st).That the month Feb is one month regardless of how many days there are in it (even though it's 28 days).So, it would mean 10 days + 1 day + 1 month total, translating to This comment was posted 1 Month and 11 Days ago.Now, if you used the MS style TimeSpan object (or any TimeSpan object in any language), it would give you the number of days from 30th Jan to 10 March (39 days), and because the TimeSpan object doesn't store relative date (the base/initial date we subtracted to get the TimeSpan), if you asked it how many months and days it has been, it will assume there is 30 days in one month, or even worst, the average which is greater than 30 days, and return the rest in days, so to get to 39 days, it will tell you it's been 1 Month and 9 Days and you will get the This comment was posted 1 Month and 9 Days ago message. Remember, both these scenarios have the same start date and same current/end date, yes the Microsoft TimeSpan object, by not allowing us to tell it the month of Feb 2013 should be be considered, has given us a completely different TimeSpan, off why a whole 2 days. It has, in effect, lied to us.

The problem is, people will believe this, and who knows what perceptions they may have, how their perceptions of the past may change and the decisions & life choices they may make when trying to reconstruct events within the past inside their own minds, while never noticing or understanding the drawback and inherent failure of representing time that is so pervasive everywhere today. They will not understand that programming languages don't realize (or care) that last month had 31 days in it, as oppposed to 30, 29 or 28 - or visa versa, and that this adds up when you increase the TimeSpan.

This is the problem at the heart of this post. I understand that most people will not care about this difference (but be sure that some of us do, and cannot have this on our backs), and if this doesn't bother you, thats ok. I wish it didn't bother me, I would have saved myself some time, stress and disappointment. If this is not a bother, you can use the function for the efficient textual display of relative time (customizable to 1 to 6 nodes from seconds to years), instead of using it for the usually negligible accuracy it provides.

To my disappointment I noticed that there is no real timespan object, if you get a timespan, and do a .years or .months you'll get nothing, you'll only get .days and lower because a timeSpan object doesn't carry anything to tell it which month or year the timeSpan was created on. Therefore it'll never really know how many months it's been since days in each month vary over a year and even further over a leap year.I figured there'd be a ...

timeSpan.GetActualNumberOf[Months/Days/Hours/etc] (base date must be provided of course)

... type method on this datatype, but there wasn't.All you'd really have to do is create another property on the timeSpan object to give it a base date on which the difference was calculated, then the above lovely string would be calculable pretty easily, and a .year & .month would exist!

View 5 Replies


ADVERTISEMENT

Convert A Number Into Equivalent Years And Months?

Dec 23, 2010

I want to get the total years and months in a number eg. 25 should give me 2 years 1 month. and 11 should give me 0 years 11 months and maybe 12 should be 1 year 0 months,i have this code but it does work for me as i want it seems the loop i am using goes only once.

Dim Month As Double = 25
Dim LeftMonth As Double
Dim Count As Integer = 0

[Code].....

View 3 Replies

Interval Of Days / Months / Years Between 2 Given Dates

Dec 21, 2011

I've been given two dates that takes their values from two respective dtpickers.I've got to calculate the exact amount of days, months and years between them.[code]The problem is that this function gives me ALL the amount of respective days, months and years passed, but I need to split them up in the correct way. Ex, between 28/2/98 and 13/1/2007 the result should be 15 days, 10 months and 8 years. And above all, the result is that the function indicates one month more (or one year more) with only one day (or month) passed between the two dates!

View 10 Replies

Calculating Exact Time In Years, MOnths, Days?

Jul 22, 2011

I am trying to calculate the exact amount of time in Years, Months and Days between 2 date values. I'm getting stuck on the day calculation. I've returned the # of months and it's value is 8.77. The .77 needs to be rounded to 8 which should give me the days. I'm not exactly sure what MATH function to use to do this.

View 39 Replies

VS 2010 Find Amount Of Years And Months Between Two Dates?

Jun 29, 2011

I have two dates, a creation date and an expiry date. I want to find out how many years and months (not days unless it is only days) between them.

I've got two dates:

Dim start_time = "12.5.2011"
Dim end_time = "15.11.2014"

I want it to say (3 Years, 6 Months) etc

View 4 Replies

C# - Calculate Difference Between Two Dates In Form `X Years, Y Months, Z Week, A Day`

Jul 10, 2011

How to get difference between two dates in Year/Month/Week/Day?

How do i calculate exact difference between to date including years, days, moths, weeks. Just like windows calculator does. ?

And represent like this 1 years, 1 months, 1 week, 1 day

View 3 Replies

Small Box Which Updates Itself Automatically To Show The Amount Of Years,months,days,hours,minutes And Seconds?

Oct 20, 2010

I have made a new form on vb 2010. I need a small box which updates itself automatically to show the amount of years,months,days,hours,minutes and seconds from the 25/6/09. How would i do this in visual basic 2010?

View 3 Replies

How To Make Previous Years Balance Become Next Years And So Forth

Apr 20, 2011

Suppose $800 is deposited into a savings account earning 4% interest compounded annually, and $100 is added to the account at the end of each year. Calculate the amount of money in the account at the end of 10 years. Determine a formula for computing the balance at the end of one year based on the balance at the beginning of the year.Allow the user to input the beginning balance and the amount to be contributed at the end of each year. You must a loop in this program. [code] I cannot figure out how to make the previous years balance become the next years and so forth.

View 6 Replies

TimeSpan Subtract Method Return Negative TImeSpan?

Dec 9, 2009

These two TimeSpan are stored in the Database with 24hr format. No date, only TimeSpan.

Dim r As TimeSpan
Dim tsStart As TimeSpan
Dim tsEnd As TimeSpan

[Code]...

Is there any TimeSpan method to get this right?

View 3 Replies

.net - Do A Foreach On A TimeSpan By Timespan Type

Apr 26, 2010

I have a requirement that regardless of the start and dates that I need to loop through that timespan and calculate figures at the month level. I cannot seem to figure it out, and maybe it is not possible, but I would like to do something like:

FOREACH Month As TimeSpan in ContractRange.Months
Do Calculations (Month.Start, Month.End)
NEXT

Is this possible or do I need to calculate the number of months, and just iterate through the amount of months and calculate the start/end of that month based on my index?

View 2 Replies

Get Dates Older Than 3 Months And Less Than 6 Months

May 23, 2012

With Vb.Net, How to get dates older than 3 months and less than 6 months ?

View 3 Replies

Componding Interest Over 10 Years?

Mar 22, 2009

I am stuck, I am doing a Case Statment and this loop to calculate pay per hour and thexpected raise percentage on 40 hr week @ 52 wks per yr. THe raise increase each amount after the first year.The display for next 10 years is the head bump. Any suggesions will be tried. In this loop trying to get the 1 year values to add the intrest rate to the following 10 years is something I can not seePrivate Sub btnCalculatePay_Click(ByVal sender As System.Object, ByVal e As ystem.EventArgs) Handles btnCalculatePay.Click

View 4 Replies

C# - Calculating Corresponding Calendar Day Of Previous Years

Mar 30, 2011

I have the following beast of an SQL (DB2) statement...

[Code]...

Taking the input date of Wednesday, 2012-03-28, this would return Wednesday, 2006-03-29. Notice that it matched the Thursdays. It matches the corresponding calendar day of previous years. Is there a more elegant way of accomplishing the same goal in C# or VB.NET? I've tried translating this query into VB.NET, but haven't been able to do so perfectly and it just looks horrible and unmaintainable.

View 1 Replies

Calculate Number Of Years To Double It?

Apr 28, 2011

I am trying to figure out how to calculate the number of years it takes to double a user defined number. I have tried to take the number entered and multiply it by twelev and divide by two.

View 2 Replies

Delete Records From The Database If They Are At-least 3 Years Old?

Dec 19, 2011

I have a project that I am working on that requires me to delete records from the database if they are atleast 3 years old.I have something like this in DB2 SQL to get the date:

SELECT * FROM tableA
WHERE ADD_DATE < CHAR(CURRENT DATE-3 YEARS)

ADD_DATE is stored as Characters in my system, this is why I am converting I know it is also possible to get the date and format it in VB.net which is the language I am using to call the SQL statements.My question is whether it would be faster/better to get the date and perform the conversion inside the SELECT in SQL or would it be better to get the current date and convert it in VB.net and then use that date in the SQL statement. I'm thinking VB.net would be better because there are thousands of records that must be compared. I should be able to set it up in VB so that it only retrieves the date and converts it once but I am not sure what kind of performance hit each takes from these statements.

View 2 Replies

Find All Leap Years Between 2 Dates?

Oct 31, 2011

I am new to VB 2010 and I am trying to right an application that takes in 2 years as inputs with in 0-3000 and displays all leap years in between and to check if the years are valid and output a message box if not.

I know that Every year that is evenly divisible by four is a leap year;

b.of those years, if it can be evenly divided by 100, it is NOT a leap year, unless

c.the year is evenly divisible by 400. Then it is a leap year.

But I cannot figure out how to put this into code. I have been looking up and trying tutorials all weekend.

View 11 Replies

VS 2008 Listing Leap Years?

Jun 10, 2009

I am having trouble with code for list box of Leap Years between a certain start date and end date.I understand the cose for finding out if a particular year is a leap year, but what I need to to is have all leap years show in a list box, once one has entered a start date (year) and an end date(year).

View 25 Replies

Adding 21 Years To A Date And Finding The Week Day

Sep 14, 2011

I am working on a code where I need to add 21 years to the date 02/15/1957 and have it display the week day of that year. I have been looking at different web sites for visual basic

View 1 Replies

Bank Future Value Investment Years Task

Mar 29, 2011

Create a project that determines the future value of an investment at a given interest rate for a given number of years. The formula for the calculation is Future value = Investment amount * (1 + Interest Rate)^ Years..Form- Use labeled text boxes for the amount of investment, the interest rate (as a decimal fraction), and the number of years the investment will be held. Display the future value in a text box formatted as currency. Include buttons for calculate, Clear, Print, and exit. Format all dollar amounts. Display a message to the user for nonnumeric or missing input data.Remember that the result of an exponentiation operation is a Double data type. Interest rate is annual.So far this is what I have and I am missing something(s) in the calculate process.[code]

View 14 Replies

Combobox/ Trying To Populate Current Year Plus The Next 10 Years?

Nov 6, 2009

I'm trying to use a combobox to pull the current year and then populate the next ten years. I don't want to manually put in the years in the 'item collections'. I would rather it update automatically based on the system year. How should I go about doing this?

My first instinct is to set the combobox item to the current year based on the system date, then create a loop that increments based on on counter up to current year + 10. I'm assuming this would be done on the 'form_load' event handler???

View 1 Replies

Merge This Three File And Create One Database For All Years?

Jan 28, 2012

Suppose you have three file of a database with information for three years (imported information of one year to database and has saved as a new file). How we can merge this three file and create one database for all years with visual basic 2010?

Note:

1-Each database has three tables and there is relationship between them

2- The ID number in tables is Auto number

3- Fields in all tables for all files are completely the same

View 2 Replies

Retrieve Only Years From Date Time Picker?

Aug 23, 2009

I am now want to retrieve only year from my table(summary),field name(dates). So i am facing the problem on how to display the year in combo Box. my sql statement as below.

[code]...

View 14 Replies

Save Actual Age (in Whole Years) For Future Use In The Program?

Jan 20, 2009

I need to save actual age (in whole years) for future use in the program. The problem is, 'Years' returns a big, fat 0 every time. Changing "Dim years As Integer" to Dim years As String" does exactly the same thing.

Code:
Dim myDate = New Date(Convert.ToInt32(Year.Value), Month.SelectedIndex + 1, Convert.ToInt32(Day.Value))
'If Month, Day, or Year not selected then

[Code]....

View 5 Replies

Setup For Hours And Years Slept Program?

Sep 18, 2009

How do you do the code setup for Hours and Years Slept Program

View 4 Replies

VS 2008 Populate ComboBox With List Of Years For DOB?

Jul 29, 2009

im creating my own control to replace the Date Calender within 2008. I want to return a list of Years into a combobox for the user to select.

Im aware i can manually add these years but ideally i would like to return a list just like the calender control.

View 3 Replies

Add 2 Months In Sql Server?

Jun 2, 2011

I'm developing a loan mng't system wherein the starting application of the customer say for example today.06/08/2011, the initial payment will be on 08/08/2011.. how am i going to that do on sql server 2005?

View 6 Replies

Add Months To A Now Function?

Jan 27, 2010

First of all i'd like to say thankyou for my last couple of threads as i've found useful ways to make my application great.

Now this is more of an extra credit function more than a necessity.

So for example if the date I currently have is 30/01/2010 how could I make that 30/02/2010? I want the program to add it on for each calandar month, if it's easier for every 4 weeks then that's fine. So is there a way to add to the "Now" function?

View 2 Replies

Determine The Name Of Months

Jul 13, 2010

I want to get the name of months base on the datediff result. What i've done is that i determine first the number of months in datediff function

Datediff(Months, "13/07/2010", "13/03/2011")

The result of that is 8. Since 8 is the result i want to convert that into months witch supposed to be:

[Code]...

View 3 Replies

How To Add Time To Timespan

Aug 16, 2010

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

[Code]...

Suppose I want to add 2 hours to this timespan what would be the method for it + code .

View 2 Replies

Subtrating From A Timespan?

Feb 6, 2010

I am not sure if this is oppsiable but i need to subtract from a timespan.

my code is

waittime = toa.Hours - drivetime
toa.hours is a integer can i change it to a decimal

View 4 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved