tags:

views:

449

answers:

3

hi all. 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

+10  A: 

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)
JaredPar
I do believe it's DateTime.Now.Year (singular). You might want to fix the typo. :-)
Ken White
Thank. Ken is right. It is singular but that's exactly what I'm looking for.
Jack
Depending on the version of VB you might want to specify "Dim year As DateTime" so that you don't get an Obejct variable.
Guffa
@Guffa, added version aware sample code
JaredPar
Uh. Just write `Dim year As New DateTime(…)`, this works regardless of the VB version and `Option Infer`.
Konrad Rudolph
@Konrad, Doh!, I get so caught up in type inference that I forget the basics
JaredPar
+1  A: 

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
dotjoe
A: 
        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
dbasnett