-1

I know it is as simple as it is, I've search around the internet and I came up with nothing. Maybe I don't know what to search for.

Basically my datetimepicker format is set to year only.
My string is "2009"
I want the datetimepicker-year to set as the year in string

StrYear = "2007"
Dim YearStart As Date = CType(StrYear , Date).Year

DateTimePicker1.CustomFormat = "yyyy"
DateTimePicker1.Value = YearStart

Error states that conversion of String "2007" to type Date is not valid

Edited by yorro: n/a

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by yorro
0
Dim StrYear As String = "1.1.2007"
        Dim YearStart As Date = Convert.ToDateTime(StrYear)
        Console.WriteLine(YearStart.Year)
0

Use ParseExact

Dim dt As Date
 Dim str as String  = "2002"
 dt = Date.ParseExact(str, "yyyy", Nothing)
 DateTimePicker1.Value = dt

Edited by __avd: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.