Question Date Problem

arsalangodil

Member
Joined
Dec 14, 2009
Messages
6
Programming Experience
Beginner
Hi,

I want biweekly start date and biweekly end date.

For Example:

If i select date from datepicker 12/14/2009 and week start day is monday then start date should be 11/30/2009 And End Date should be 12/13/2009.

And if i select date from datepicker 12/15/2009 and week start day is monday then it should show start date 12/14/2009 not 12/1/2009 or 12/7/2009.

I m totally confused...please help..
 
{Be hAppy}

Dim StartWeekDay As String = "Mon"
Dim NumberOfDaysInWeek = 7
Dim StartDate As Date = Nothing
Dim EndDate As Date = Nothing
Dim MyDate As Date = DateSerial(2009, 12, 15)
Do While Format(MyDate, "ddd") <> StartWeekDay
MyDate = DateAdd(DateInterval.Day, -1, MyDate)
Loop
StartDate = MyDate
EndDate = DateAdd(DateInterval.Day, NumberOfDaysInWeek - 1, MyDate)
MsgBox(StartDate)
MsgBox(EndDate)
 
{Be hAppy}

Dim OneOfTheDayRequired = DateSerial(2009, 11, 30)
Dim StartWeekDay As String = "Mon"
Dim NumberOfDaysInWeek = 7
Dim StartDate As Date = Nothing
Dim EndDate As Date = Nothing
Dim MyDate As Date = DateSerial(2009, 12, 14)
Do While Format(MyDate, "ddd") <> StartWeekDay Or DateDiff(DateInterval.Day, OneOfTheDayRequired, MyDate) Mod 14 <> 0
MyDate = DateAdd(DateInterval.Day, -1, MyDate)
Loop
StartDate = MyDate
EndDate = DateAdd(DateInterval.Day, NumberOfDaysInWeek * 2 - 1, MyDate)
MsgBox(StartDate)
MsgBox(EndDate)
 
Hi send2te,

I am working on periods Daily, Weekly, BiWeekly, SemiMonthly, Monthly.

I have two more questions.

In SemiMonthly i have day periods defined like 1 to 16 and 2 to 17. I want to make start date like 1/1/2009 and end date like 1/16/2009 but if a month have 30 days then next period start date should be 1/17/2009 and 1/30/2009.

In Monthly i have initial day defined like i want to start month from 2nd then the month start date should be 1/2/2009 and end date should be 2/1/2009.

Thnks in advance..
 
{Be hAppy}

Dim StartDate As Date = Nothing
Dim EndDate As Date = Nothing
Dim MyDate As Date = DateSerial(2009, 12, 14)
If MyDate.Day < 17 Then
StartDate = DateSerial(MyDate.Year, MyDate.Month, 1)
EndDate = DateSerial(MyDate.Year, MyDate.Month, 16)
Else
StartDate = DateSerial(MyDate.Year, MyDate.Month, 17)
EndDate = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, MyDate))
End If
 
{Be hAppy}

There is one mistake in already provided code:

Use the following one instead:

Dim StartDate As Date = Nothing
Dim EndDate As Date = Nothing
Dim MyDate As Date = DateSerial(2009, 12, 14)
If MyDate.Day < 17 Then
StartDate = DateSerial(MyDate.Year, MyDate.Month, 1)
EndDate = DateSerial(MyDate.Year, MyDate.Month, 16)
Else
StartDate = DateSerial(MyDate.Year, MyDate.Month, 17)
EndDate = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, DateSerial(MyDate.Year, MyDate.Month, 1)))
End If
 
EndDate = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, DateSerial(MyDate.Year, MyDate.Month, 1)))

You really need to start using the .NET functionality, send2te - it makes code much simpler and also easier to read:-

VB.NET:
EndDate = New Date(Mydate.Year, Mydate.Month, 1).AddMonths(1).AddDays(-1)

or even easier still :-

VB.NET:
EndDate = New Date(Mydate.Year, Mydate.Month, Date.DaysInMonth(Mydate.Year, Mydate.Month))
 
Nearly forgot the faster typing option :D

VB.NET:
With Mydate
    EndDate = New Date(.Year, .Month, Date.DaysInMonth(.Year, .Month))
End With
 
{Be hAppy}

Dear IntertiaM

Thats great. Thanks for your comments. I realy need to start exploring new things ...

Off course easy syntax can make like easy ... and in future I will adopt the same as you mentioned.

In fact I am logical but lazy and explore some thing upto my requirements.

One somebody asked what to do to get rounded value upto 0 decimal place ...
At this time I know only int() function that was taking integer portion only.
v=3.4
int(v) = 3
v=3.7
and int(v)=3

So without exploring the actual function I told him to use

int(v+0.5) will give you always a rounded value upto 0 decimal places ...

Once a guy who was expert in algorithm asked to write a software that will accept always 4 charcter string and present all 16 different combinations ...


So If input was "Send" I need to produce remaining 15 strings like "endS", "Snde" etc.

It was not simple program and I need to explore proper algorithm for this ....

But I can not easily explore some new thing ... so what I have written

X="Send"
Dim A(4)
A(1)="S"
A(2)-"e"
A(3)="n"
A(4)="d"
for i=1000 to 9999
if i contains 1 and i contains 2 and i contains 3 and i contains 4 then


msgbox A(val(mid(i,1,1)) & A(val(mid(i,2,1)) & A(val(mid(i,3,1)) & & A(val(mid(i,4,1))



end if
next
 
I realy need to start exploring new things ...

At this time I know only int() function that was taking integer portion only.
v=3.4
int(v) = 3
v=3.7
and int(v)=3

int(v+0.5) will give you always a rounded value upto 0 decimal places ...

Yes, in old VB6 or below, that would have been fine. But programmers who are more intelligent than me ;) have thought about all the 'tricks' we used to have to write, and have now coded them for us :-

VB.NET:
Math.Ceiling(3.4) = 4
Math.Ceiling(3.7) = 4

I think it's time you removed your Microsoft.VisualBasic reference. I know it will make writing code difficult for a while (I remember when I decided to go from VB6 to 2005 :eek: ), but you will write better code afterwards :D
 

Latest posts

Back
Top