Keroleen Posted November 18, 2003 Posted November 18, 2003 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: Quote
Administrators PlausiblyDamp Posted November 18, 2003 Administrators Posted November 18, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Keroleen Posted November 19, 2003 Author Posted November 19, 2003 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 :~( Quote
Administrators PlausiblyDamp Posted November 20, 2003 Administrators Posted November 20, 2003 Which line are you getting that error on? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Keroleen Posted November 20, 2003 Author Posted November 20, 2003 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??? Quote
Administrators PlausiblyDamp Posted November 20, 2003 Administrators Posted November 20, 2003 Could you show a bit more of the code? How are you reading the value from the DatetimePicker etc. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Keroleen Posted November 21, 2003 Author Posted November 21, 2003 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. Quote
Administrators PlausiblyDamp Posted November 21, 2003 Administrators Posted November 21, 2003 Try dateTemp = Date.Parse(txt_beginning.Text) or failing that have a look in MSDN for the ParseExact method. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Keroleen Posted November 27, 2003 Author Posted November 27, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.