Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Whole-number part of date (.net)

    How do I determine the whole-day part of a date? I have used dateVar.Hour to find the 0-23 hour in the day, but the day part has me stumped. In VB6 or VBA, I would use Int(dateVar), but that expression causes a run-time error. Tried CInt() and CLng, but they will not compile. Tried dateVar.Date and dateVar.DayOfYear without success.

    TIA
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Whole-number part of date (.net)

    Why do you want to work directly with this number? Playing around with VBA, in the Immediate window, I get:

    ?clng(now)
    38485
    ?dateadd("d", -clng(now), now)
    12/29/1899 4:00:53 PM

    But in .NET Microsoft is going much, much further back, so you are dealing with extremely huge numbers:
    <hr>The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) to 11:59:59 P.M., December 31, 9999 A.D. (C.E.)

    Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 1 A.D. (C.E.) in the GregorianCalendar calendar. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.
    DateTime Structure<hr>
    You could check out what you get for dateVar.Ticks, but... I think you are swimming against the tide a bit. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Whole-number part of date (.net)

    If datVar is declared as a System.DateTime, then datVar.DayOfYear should work or datVar.Day for the day of month. Try this aircode and see what you get:

    Dim datVar as System.DateTime = DateTime.Now
    Dim intDay As Integer = datVar.Day

    Can you post the snippet of code that is failing and tell us what error you get?
    Charlotte

  4. #4
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Whole-number part of date (.net)

    The data are GPS readings from mobile equipment. I need to do some calculations on a daily basis (sums, averages, etc), so I thought I could use the whole-number part of the date in the GROUP BY clause of an SQL statement. Using the Int() function in VBA in Access and Excel, I had been used to seeing numbers in the 38000 range, which would handle any roll-over from year-to-year. The dateVar.DayOfYear returns numbers in the 0-365 range, which may cause a problem with data collected, say, in December and January.

    So if I understand you correctly, the old VBA concept of how a date variable was composed of a whole-day part and a fractional-day part is out the window? It sounds like you are right about the swimming against the tide.

    Thanks for the info.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Whole-number part of date (.net)

    With the dateVar.DayOfYear, it was not that an error was generated, it was that the numbers were in the 0-365 range, rather than the 38000 range that the Int() function of a VBA date would return. This difference has implications for the analysis that I need to do on multi-year data -- trying to get sums and averages for data grouped by day.

    From Jefferson's reply, it is apparent that I have bumped into a difference in the way dates are defined internally between .NET and VBA, and will have to rethink my approach to this analysis.

    the computer that I am on right now doesn't have .NET installed, so I can't post a snippet.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  6. #6
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Whole-number part of date (.net)

    You can use a TimeSpan object to calculate the number of days between two dates.

    Try this code.

    Dim span As TimeSpan
    Dim fromdate, todate As Date

    fromdate = Date.MinValue
    todate = Now

    span = todate.Subtract(fromdate)

    Debug.WriteLine("Days from " & fromdate.ToString & " until " & todate.ToString & " = " & span.Days)

    The assignment of Date.MinValue to the fromdate variable allows you to use a consistant starting point for the number of days.

    Regards,
    Kevin Bell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •