VB.NET Get Only Year from Date

95,453

Solution 1

Date.Today.Year is what you're looking for, or for an existing date, just someDateVariable.Year

Solution 2

Dim myDate As DateTime = #1/1/2011#
Dim myYear As Int32 = myDate.Year

Solution 3

i know this is a bit late to answer but, it wont hurt telling, try "year(now) " and load it to a variable Example:

Dim Year_in_Digits = Year(Now)

Solution 4

I just had to do this for my VB program.

 Dim Year As Integer    
 Year = Convert.ToInt32(Now.ToString("yyyy"))

Use just "yy" if you want two digit year. Then, when you need to display it somewhere:

year.tostring

Solution 5

There is a property on the DateTime structure called 'Year'. This returns an integer representing just the year.

If you need to convert this to a string, just use the ToString() function.

So..

MyDT.Year.ToString()

That's a c# example, I'm sure VB.Net is going to be very similar.

Share:
95,453

Related videos on Youtube

Dave Mackey
Author by

Dave Mackey

Love to code: Python, JS, C#, PHP, SQL, VB.NET, HTML, CSS. In ancient days I coded in QuickBasic, ASP, and VBScript. I'm a friendly introvert with solid communication skills. I work hard and have refined problem solving skills. When I was younger I worked in a variety of industries (commercial fisherman, stone mason, lawn care, factory, custodial, youth leader). I settled on IT and have experience working in a startup, higher education, and with non-profits.

Updated on March 20, 2020

Comments

  • Dave Mackey
    Dave Mackey about 4 years

    I'm pulling into a variable a datetime value. Now, I want to post this value back to my database, but I need it to be only the year digits. How do I get VB.NET to trim the month, day, and time off and just return the four character year (e.g. 2011)?

  • Dave Mackey
    Dave Mackey about 13 years
    I get the message: "Year is not a member of 'System.Linq.IQueryable(Of Date?)"
  • Dave Mackey
    Dave Mackey about 13 years
    I figured it out, I needed to make it: getYear.First.Value.Year

Related