If I wanted to determine today's date based on my birthdate, how could I do that mathematically?

112 Views Asked by At

Let's say for example my birthday is August 17, 1996. Each year has 365 days except leap years. If I didn't know whether 1996 was a leap year or not, is there a way I can see how many days I've been alive (obviously without looking at a calendar or manually counting). And from my birthdate, how can I (if possible) deduce what today's date is?

4

There are 4 best solutions below

0
On BEST ANSWER

I wrote a small program (I am a beginner programmer, so it may not be a very efficient way of doing this) to compute the number of days passed between two dates. One would enter the first date in the form D/M/Y and then the second in the same form, and this will spit out the number of days in between. I used C++.

#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <utility>
#include <sstream>
#include <cmath>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iterator>
using namespace std;

//Leap year check
bool leapyearcheck (long double y)
{
    if ((y/4==floor(y/4)) && (y/100!=floor(y/100)))
    {
        return true;
    }
    else if (y/400==floor(y/400))
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Month counter
long double counter_month (long double d, long double m, long double y)
{
    if (m==1)
    {
        return 0;
    }
    else if (m==2)
    {
        return 31;
    }
    else if ((m==3) && (leapyearcheck(y)==true))
    {
        return 60;
    }
    else if ((m==3) && (leapyearcheck(y)==false))
    {
        return 59;
    }
    else if ((m==4) && (leapyearcheck(y)==true))
    {
        return 91;
    }
    else if ((m==4) && (leapyearcheck(y)==false))
    {
        return 90;
    }
    else if ((m==5) && (leapyearcheck(y)==true))
    {
        return 121;
    }
    else if ((m==5) && (leapyearcheck(y)==false))
    {
        return 120;
    }
    else if ((m==6) && (leapyearcheck(y)==true))
    {
        return 152;
    }
    else if ((m==6) && (leapyearcheck(y)==false))
    {
        return 151;
    }
    else if ((m==7) && (leapyearcheck(y)==true))
    {
        return 182;
    }
    else if ((m==7) && (leapyearcheck(y)==false))
    {
        return 181;
    }
    else if ((m==8) && (leapyearcheck(y)==true))
    {
        return 213;
    }
    else if ((m==8) && (leapyearcheck(y)==false))
    {
        return 212;
    }
    else if ((m==9) && (leapyearcheck(y)==true))
    {
        return 244;
    }
    else if ((m==9) && (leapyearcheck(y)==false))
    {
        return 243;
    }
    else if ((m==10) && (leapyearcheck(y)==true))
    {
        return 274;
    }
    else if ((m==10) && (leapyearcheck(y)==false))
    {
        return 273;
    }
    else if ((m==11) && (leapyearcheck(y)==true))
    {
        return 305;
    }
    else if ((m==11) && (leapyearcheck(y)==false))
    {
        return 304;
    }
    else if ((m==12) && (leapyearcheck(y)==true))
    {
        return 334;
    }
    else if ((m==12) && (leapyearcheck(y)==false))
    {
        return 335;
    }
}

//Year counter
long double counter_year (long double d, long double m, long double y)
{
    long double a=0;
    long double b=0;
    for (long double j=0;j<y;j++)
    {
        if (leapyearcheck(j)==true)
        {
            a++;
        }
        else if (leapyearcheck(j)==false)
        {
            b++;
        }
    }
    return 366*a+365*b;
}

//Main counter
long double counter_main (long double d, long double m, long double y)
{
    return counter_month(d,m,y)+counter_year(d,m,y)+d;
}

//Days difference
long double daysdiff (long double d_1, long double m_1, long double y_1, long double d_2, long double m_2, long double y_2)
{
    return counter_main(d_2,m_2,y_2)-counter_main(d_1,m_1,y_1);
}

//String to date
vector<long double> stodate (string input)
{
    long double d,m,y;
    istringstream ss(input);
    string entry="";
    int index=0;
    while (getline(ss,entry,'/'))
    {
        if (index==0)
        {
            d=stold(entry);
        }
        else if (index==1)
        {
            m=stold(entry);
        }
        else if (index==2)
        {
            y=stold(entry);
        }
        index++;
    }
    vector<long double> date(3);
    date[0]=d;
    date[1]=m;
    date[2]=y;
    return date;
} 

//Output
int main()
{
    long double d_1,m_1,y_1,d_2,m_2,y_2;
    string startdate, enddate;
    cout << "Enter the start date (D/M/Y): ";
    cin >> startdate;
    cout <<"\nEnter the end date (D/M/Y): ";
    cin >> enddate;
    d_1=stodate(startdate)[0];
    m_1=stodate(startdate)[1];
    y_1=stodate(startdate)[2];
    d_2=stodate(enddate)[0];
    m_2=stodate(enddate)[1];
    y_2=stodate(enddate)[2];
    cout << "\nThe number of days passed is: " << daysdiff(d_1,m_1,y_1,d_2,m_2,y_2);
}

I create functions to count the number of days produced by the number of years, months, and then days of each date. Then I subtract the two numbers obtained from each date to obtain the number of days passed between the two dates. The number of days for the months is $31$ if it is February, for instance (i.e. if someone inputs 2 as the month, then $31$ days have passed since the beginning of that year until this month). The only tricky part is with leap years, in which a leap year occurs if $$(4\mid y~\land~100\nmid y)\lor(400\mid y)$$where $y$ denotes the year.

For your birthdate: August 17, 1996, we would input 17/8/1996 as the start date, and then today's date as the end date (the day I posted this is 30/7/2017). On the day I posted this, you would be $7652$ days old.

0
On

I don't know if this is an acceptable answer, but you can do 1) An SQL Server query using the DateDiff function, a "" function with inputs ( DatePart, Date 1, Date 2) where DatePart is the "Gradation" : Years, Months, Days, etc.

Select DateDiff( Days, Date1, Date2) , where Date1 is your DOB, Date2 is the date for which you want to find the number of days passed. You can run it for free here: https://www.w3schools.com/sql/func_datediff.asp

OR 2) Use a perpetual calendar https://accuracyproject.org/perpetualcalendars.html: There are 14 possible day arrangements for a calendar: Once the date of January 1st and whether the year is a leap year or not, the calendar is determined : Jan 1st can fall on Mon through Sunday , and the year is either leap or non-leap. Just look up the year number and look up the calendar for that year.

0
On

If you want to be able to do that in your head, then you'd start by working out the number of the day of both dates (as in, January 2nd would be day #2, December 30th in a non-leap year would be day #364, etc.).

You can do this fairly quickly by memorizing the number of days that occur prior to each month in a non-leap year.

Before January, there are 0 days.
Before Februrary, there are 31 days.
Before March, there are 59 days.
Before April, there are 90 days.
Before May, there are 120 days.
Before June, there are 151 days.
Before July, there are 181 days.
Before August, there are 212 days.
Before September, there are 243 days.
Before October, there are 273 days.
Before November, there are 304 days.
Before December, there are 334 days.

These amounts of days are easy to memorize. All of them, not surprisingly, are close to their month number (January = 1, February = 2, etc.) minus 1 times 30. For leap years, of course, just add 1 to every month from March to December.

Using your example of August 17th, 1996, you'd first need to realize that 1996 is a leap year. Before August, there are 212 days before it, plus 1 for the leap day, making 213 days. Naturally, this makes August 17th, 1996 the 230th (213 + 17 = 230) day of the year.

As I write this, it's July 27, 2017, so let's figure out when the next July 27th would be after your start date. That would be July 27th, 1997. July 27th is the 208th day of the year (181 days before July, plus 27 days in July).

From the 230th day of a leap year to the end of the year (the 366th day) is 136 days. Adding another 208 days to that gives us 344 days between the 2 dates.

So, from August 17th, 1996 to July 27, 1997 is 344 days. July 27th, 2017 is 20 years after that, so if you desired, you could simply state that it's been exactly 20 years and 344 days since August 27, 1996.

To count the total number of days, you'd have to multiply 365 by 20 (which is 730 × 10 or 7300 days). Finally, you'd have to add 1 for each leap year between the 2 dates: 2000, 2004, 2008, 2012, and 2016. You get 7300 + 5 + 344 = 7649 days between the 2 dates. (Wolfram|Alpha calculation)

Of course, you'd have to practice enough to become comfortable with multiplying 365 times various integers to use this latter approach.

2
On

There is a very simple solution (assuming that you can use programs) : convert dates to Julian days and subract the two numbers.

For example, in the book "Numerical Recipes" you could find a subroutine named julday.

You also could find a source code here.

For example, today $(07/28/2017)$ is $2457963$ and your birthday $(08/17/1996)$ is $2450313$ which makes a difference of $7650$.

You can use the method for any other situation.