+ Reply to Thread
Results 1 to 2 of 2

Thread: Date subtraction

  1. #1
    Registered User
    Join Date
    11-18-2010
    Location
    Mt. Maunganui, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    2

    Question Date subtraction

    Hi
    I am trying to convert a function from a VB (version 6.5) macro in Excel 2007 to c#. But there is a date calculation which I don't understand. Can someone please tell me what the following does?

    Function CalcRate(D As Date) As Double
      Dim B, Dmin1 as Date, Dplus1 as Date
    
      Dmin1 = DateSerial(Year(D), 1, 1)
      Dplus1 = DateSerial(Year(D) + 1, 1, 1)
    
      B = (Dplus1 - D) / (Dplus1 - Dmin1)
    etc

    Thanks,
    Peter

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Date subtraction

    Hi,

    The user types =CalcRate(date) in Excel, where date is a valid date number.

    The function evaluates two date variables. Dmin1 evaluates to the 1st January for the year containing the date passed, and the Dplus1 evaluates to the 1st January for the following year. So if the date passed is say 13 March 2009, Dmin1 = 1 Jan 2009 and DPlus1 = 1 Jan 2010.

    The B variable takes the number of days between the date passed and the next 1st Jan and divides this by the number of days between 1st Jan in the date year and the 1st Jan in the following year.

    i.e. it's working out the % of the calendar year that's left as a decimal.

    HTH
    Last edited by Richard Buttrey; 11-18-2010 at 07:57 PM.
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0