Jack
Jack

Reputation: 10037

VB.net: How do you get the date of the begining year?

Today is 3/12/2009 and based on today's date. I want to get the start date of this year and that will be 1/1/2009. Does anyone know if there is a way to do this? The reason for this is i want to get the DateDiff of today's date against the start date of today's date's year.

Jack

Upvotes: 2

Views: 15305

Answers (3)

JaredPar
JaredPar

Reputation: 754545

Try this

EDIT As Konrad pointed out, just keep it simple

VS Any version

Dim year As New DateTime(DateTime.Now.Year, 1, 1)

VS2008 (with Option Infer On )

Dim year = new DateTime(DateTime.Now.Year, 1, 1)

VS2005 or VS2008 (with Option Infer Off)

Dim year As DateTime = new DateTime(DateTime.Now.Year, 1, 1)

Upvotes: 14

dbasnett
dbasnett

Reputation: 11773

        Dim day1 As DateTime = New DateTime(DateTime.Now.Year, 1, 1)
    Dim ts As TimeSpan
    '
    ts = DateTime.Now - day1
    '
    Label1.Text = ts.Days.ToString
    'or
    Label1.Text = ts.TotalDays.ToString

Upvotes: 0

dotjoe
dotjoe

Reputation: 26940

There is the DayOfYear date method in answer to your last question...

Dim d As New Date(2009, 3, 12)

Console.WriteLine(d.DayOfYear) '71
Console.WriteLine(DateDiff(DateInterval.Day, New Date(d.Year, 1, 1), d)) '70

Upvotes: 1

Related Questions