-1

How to get the number of the week in datetimepicker just like in Mysql just use the function WEEK() and it will return the week number, simple as that.

example.

datetimepicker1.text = "9/9/2014"
'returns 36

just like in mysql. example.

WEEK(9/9/2014)
`returns 36
Mharveen Biu
  • 215
  • 1
  • 3
  • 12
  • Get its date value then: [Get the correct week number of a given date](http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date) – Alex K. Sep 09 '14 at 11:56

1 Answers1

3

You can use Calendar.GetWeekOfYear

Dim dt = datetimepicker1.Value 
Dim dateInfo = Globalization.DateTimeFormatInfo.CurrentInfo
Dim c = dateInfo.Calendar

Dim week = c.GetWeekOfYear(dt, dateInfo.CalendarWeekRule, dateInfo.FirstDayOfWeek)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • gives me error, when i insert datetimepicker1.text onto dt = newdate() – Mharveen Biu Sep 09 '14 at 12:08
  • What is `newdate()`? Also, why do you assign a `Date` to a `String` property? Use the `DateTimePicker.Value`-property: `datetimepicker1.Value = New Date(2014, 9, 9)`. However, i don't know how this is related to the weeknumber question at all. – Tim Schmelter Sep 09 '14 at 12:08
  • i inserted the dim dt = new date (datetimepicker).. nevermind. i manage to get the weeknumber, theres something weird happened. 9/9/2014 in vb.net is 37 but in mysql is 36. what just happened? – Mharveen Biu Sep 09 '14 at 12:10
  • @MharveenBiu: if you want to convert a `Date` to string use `dt.ToString()`. I have edited my answer to clarify how you get the date from the `DateTimerPicker`. – Tim Schmelter Sep 09 '14 at 12:12
  • whats the problem between the mysql and vb.net time? mysql give me 36 and vb.net give me 37? – Mharveen Biu Sep 09 '14 at 12:21
  • @MharveenBiu: i assume because the `DateTimeFormatInfo` uses the rules of your current culture(incl. `FirstDayOfWeek` or [`CalendarWeekRule`](http://msdn.microsoft.com/en-us/library/system.globalization.calendarweekrule(v=vs.110).aspx)) whereas [`WEEK`](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_week) just has one parameter for when the week starts(Sunday, Monday,...). Default is Sunday if you omit that parameter. – Tim Schmelter Sep 09 '14 at 12:26
  • theres no fix for that? im using this two because i want to select the week using datetimepicker and return the records in mysql in that week and group by it. if i use a simple math -1 in vb.net and pass the parameter to mysql. do you think it will work? or any other solutions? @TimSchmelter – Mharveen Biu Sep 09 '14 at 12:32
  • 1
    @MharveenBiu: _What_ do you want to fix? The `MySql`-`WEEK`-function? You could start by providing the correct week-start(monday here in germany, so 1): `WEEK(NOW(),1)` returns 37 – Tim Schmelter Sep 09 '14 at 12:45