Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I want to be able to display the first day of the week and the last day of the week. I have set the first day of the week to be vbSaturday and last day is vbFriday.

 

The user will use the datetimepicker tool to pick a day. This can be any date. And based on that date, the system should calculate the first day of the week.

 

ie. If the user picks November 11, 2003 (Tuesday) Then the textbox should display Saturday, November 8, 2003 as the first day of the week and Friday, November 14, 2003 as the last day.

 

Actually to make things simple.....I would like to know how to convert this VBA code to VB.net code

 

Dim dateTemp as Date

'subtract days from selected date until the date is saturday

Do Until DatePart ("w", dateTemp) = vbStaurday
    dateTemp = dateTemp - 1 
Loop

txtBeginningDate = dateTemp

'find the last day of the week
txtEndingDate = dateTemp + 6

 

The output should be in date format ~~ "18-Nov-03"

 

Thanks in advance

:confused: :confused: :confused: :confused:

  • Administrators
Posted

Dim dateTemp As Date

'subtract days from selected date until the date is saturday
Do Until dateTemp.DayOfWeek = DayOfWeek.Saturday
dateTemp.Subtract(TimeSpan.FromDays(1))
Loop

txtBeginningDate.Text = dateTemp.ToLongDateString()

'find the last day of the week
txtEndingDate.Text = dateTemp.AddDays(6).ToLongDateString()

 

or you could change the loop to

Do Until dateTemp.DayOfWeek = Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FirstDayOfWeek
      dateTemp.Subtract(TimeSpan.FromDays(1))
Loop

instead of hard coding the first day of the week.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thanks for your reply.

 

But is there another function besides using TimeSpan?

Because I am using a datetimepicker . And it gives me this error

 

Additional information: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks

 

 

:~(

Posted

           dateTemp.Subtract(TimeSpan.FromDays(1))

 

 

I commented this line out

 

'dateTemp = txt_beginning.Text

 

But I should be using this because what I am doing is taking the date that the user picked via DATETIMEPICKER tool and passing hte variable over to my current form which im doing this little calculation.

 

txt_beginning.text is a text field with a full date (11-Nov-03) as a text.

 

as indicated dateTemp is a date field....so i guess i would need to a date->text conversion if it is at all possible???

Posted

On Form1 :

' when the user clicks on a date to insert that into the date textbox
       txt_date.Text = PickedDate.ToString("dd-MMM-yy")
       Date1 = System.DateTime.Compare(PickedDate, FirstDay)
......

Dim d1 As Date

       'Beginning date of the week
       d1 = txt_date.Text
       tempdate = Weekday(d1, FirstDayOfWeek.Saturday)

 

This reads the date from the userinput and I will pass the variable (txt_beginning.text) to the second form

 

   frm.txt_beginning.Text = tempdate 

 

On the 2nd form:

 

Dim endingdate As String

Dim dateTemp As Date

 

'dateTemp = txt_beginning.Text

 

Im trying to assign the value of the date picked into the new variable dateTemp

 

 

....

 

and then the Do Until loop.

Posted

I resolved this:

 

'for beginning date

       strTemp = CDate(txt_beginning.Text)
       strTemp2 = CDate(txt_beginning.Text)
       Do Until strTemp.DayOfWeek = DayOfWeek.Saturday
           strTemp = DateAdd(DateInterval.Day, -1, strTemp)
       Loop

       txt_beginning.Text = strTemp


       'for ending date
       Do Until strTemp2.DayOfWeek = DayOfWeek.Friday
           strTemp2 = DateAdd(DateInterval.Day, 1, strTemp2)
       Loop

       txt_ending.Text = strTemp2


 

 

Thanks for your help along the way.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...