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

Recommended Answers

All 3 Replies

Dim StrYear As String = "1.1.2007"
        Dim YearStart As Date = Convert.ToDateTime(StrYear)
        Console.WriteLine(YearStart.Year)

Use ParseExact

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

Thank you GeekByChoice and Adatapost for your replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.