Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Get Only Year from Date

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)?

like image 889
Dave Mackey Avatar asked Apr 05 '11 23:04

Dave Mackey


4 Answers

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
like image 155
Insub Avatar answered Oct 02 '22 22:10

Insub


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

like image 20
ScottE Avatar answered Oct 06 '22 15:10

ScottE


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)
like image 4
jude.MMMM Avatar answered Oct 06 '22 14:10

jude.MMMM


Dim myDate As DateTime = #1/1/2011#
Dim myYear As Int32 = myDate.Year
like image 3
Brandon Montgomery Avatar answered Oct 06 '22 15:10

Brandon Montgomery