Calculating Months - vb.net

I have an application where the user selects the dates of a first statement and a last statement. Example, first statement = 1/1/08, last statement = 12/1/08, should equal 12 statements.
However, when using the following code, the result is 11:
numPayments = DateDiff(DateInterval.Month, CDate(.FeeStartDate), CDate(.FeeEndDate))
Is there another way to calculate this, or do I have to be stuck with adding 1 to the result?

Add 1, as you write. ;)
The difference between 1/1/2008 and 12/1/2008 is 11 months. No changing that. ;)

Yes, you'd always have to add one though you may be able to add one to the end date or subtract one from the start date to also get this effect. Consider the case where the start and end dates are the same. Their difference is 0 but you'd still want 1 statement to show just to note one odd case.

Well, the number of months between Jan 1st and Dec 1st is 11... what you're looking for is the difference of months +1. So just add one :)

Also, the DateDiff function you're using is a VB6 hold-over. Better to express it like this:
numPayments = (Date.Parse(.FeeEndDate) - Date.Parse(.FeeStartDate)).TotalMonths + 1

You could try this one. Hope this is very helpful.
Dim myDate As Date
Dim dateNow As Date
Dim nextMonth As Date
myDate = Now
dateNow = Format(myDate, "MM/dd/yyyy")
nextMonth = DateAdd(DateInterval.Month, 5, dateNow) 'compute the next 5 months from date now. Let say, #12/6/2012# the result will be #5/6/2013#
MessageBox.Show(DateDiff(DateInterval.Month, dateNow, nextMonth) & "months==> " & nextMonth)
'This will count the number of months interval. The result will be 5 months=>> #5/6/2013 because we count december to may.

Related

MS SQL How to get the next month if its december

Hi I wanna get the next month in SQL server but what if the month is 12.
when i have date = '2016-10-04' then the next month will be date = '2016-11-04'.
I want to put this into this query :
if EXISTS(
select * from month
where id_Prod = #id_Prod
and datepart(month,DATEADD(month,1,_date)) = datepart(month,DATEADD(month,1,_date))
and datepart(YEAR,_date) = datepart(YEAR,#date)
);
you can try dateadd
declare #dt date = getdate()
select datepart(MM,dateadd(mm,1, #dt))
If the spec I've given in the comments is correct, you want something along the lines of:
if EXISTS(
select * from month
where id_Prod = #id_Prod
and _date >= DATEADD(month,DATEDIFF(month,'20010101',#date),'20010201')
and _date < DATEADD(month,DATEDIFF(month,'20010101',#date),'20010301')
);
The DATEADD,DATEDIFF pairs are just being used to generate "the 1st of next month" and "the 1st of the month after that", using arbitrary (fixed) dates to compute those. E.g. the first line computes how many whole months have occurred between 1st January 2001 and #date. It then adds that number of months onto 1st February 2001. This expression should therefore always generate the 1st of the month that comes after #date. The second pair does the same but adds the computed number onto 1st March instead.
You should also note that I'm not applying any functions to _date, so if there happens to be a useful index on that column, it should be usable for this query.
This seems pretty simple,
Get Previous date of current date
SELECT DATEADD(MONTH,-1,GETDATE()) AS PrviousDate
Get Next date of current date
SELECT DATEADD(MONTH,1,GETDATE()) AS NextDate

What exactly happens if I add two dates in VBA

Please help me what happend here- I know it is stupid but I want to know.
The output of the below code is April-18-2105. How did it pop up?
Private Sub CommandButton1_Click()
Dim firstDate As Date, secondDate As Date
firstDate = DateValue("Jun 19, 2010")
secondDate = DateValue("oct 29,1994")
MsgBox (firstDate + secondDate)
End Sub
Thanks in advance!
Dates are actually stored as numbers, so if we look at the numeric value for these dates:
CLng(datevalue("Jun 19, 2010")) '// 40348
CLng(datevalue("oct 29, 1994")) '// 34636
and add them together:
40348 + 34636 = 74984
and convert that number back to a date:
CDate(74984) '// 18/04/2105
Because it's 74,984 days after 00/01/1900
dates are numbers, formatted as dd/mm/yyyy, I think its the days from 31/12/1899, so that's what's happening. The result is 40348+34636=74984, which is 18/04/2105.
Excel stores the date as the number days after a specified date. As such the integer representing the date of 2010/06/19 is 40348 and the integer representing 1994/10/29 is 34636. When you add those integers together you get 74984 which is the integer that represents 2105/04/18.
The reference date does vary from mac to windows. I am running on windows and I did not change my reference date settings, That is where those integers came from. Mac uses January 2nd 1904 as day 1 and Windows uses January 1 1900 as day 1.

Out of a set of data returned grouped by months, only the initial month is returning incorrect data

I am trying to return data for the last 24 months as of the end of last month. Only the initial month returns incorrect data, while all the other months are correct. I believe the issue is in the strBeginDate = section of the code. Any ideas what would be causing partially returned data for the initial month? Thank you very much.
Sub GetStaticApprovalRates_Slide6()
Dim strBeginDate
Dim strEndDate
strEndDate = Sheets("Instructions").Range("EndDate").Value
strBeginDate = DateAdd("m", -23, strEndDate) + 1
Sheets("Slide6Data").Select
It's hard to say exactly what's wrong based only on what you posted. But I do see that you are calculating the start date based on the end date, by only subtracting months. There is no allowance for days. So you might be missing some of that first month by not allowing for the early days of that first month.
That is , if end date occurs mid-month, I think your algorithm would cause start date to be mid-month also. Perhaps missing days 1-x of that first month.

webmatrix - sql query using DateTime.Now

i am trying to write a sql query in Webmatrix to show the count of entries in my database within the last month. I have written the below query, but it's giving me a count of zero, when i know that's not true. I think i may be writing the wrong code to show the last 7 days?
var lastweek = DateTime.Now.AddDays(7);
var week = "SELECT COUNT (*) FROM PropertyViews WHERE PropertyID = #0 AND ViewTimestamp >= #1";
var qweek = db.QueryValue (week, rPropertyId, lastweek);
You are not looking at last week, but at the coming week (today + 7 days is in the future!)
Try
var lastweek = DateTime.Now.AddDays(-7);
You mention last month by the way, but i guess you mean week everywhere :)

return week number based on date

I am new to T-SQL and needed urgent assistance here.
I am trying to get the week number from a given date.
I understand that there is a build in function for it but the value return is not exactly what I wanted.
For e.g., by using select datepart(wk, '2013-01-07'), it would return me '2'.. but the actually fact is it should return '1' instead of '2'.
Any ideas how to correct this issue?
You can use dy datepart specifier to get dayOfYear number and divide it by 7:
select (datepart(dy, '2013-01-05') - 1) / 7 + 1;
Working DEMO.
Try this
SELECT DATEPART(WEEK,GETDATE())
This depends on hop you define the first week. Does it always start on the same weekday? or does it always start on the first of January? If you want it to always start on the same weekday, then use Set datefirst to tell T-SQL what weekdaty you want to define as the start of the week. If you want it to always start on Jan 1, then just use day of year instead of week, subtract 1, integer divide by 7 and add 1.
declare #dat DateTime = getdate()
Select Select (datepart(dy, #dat)-1) / 7 + 1
Although going from memory, I believe the ISO standard for the first week of the year is the week in the year that the first Thursday of the year is in. This would possibly explain why the built in function gives a result different to that you require.