Difference between revisions of "Year-Month-Day-to-Day"

From Tech
Jump to navigationJump to search
(Created page with "Sometimes you want to convert a gregorian year-month-day to a simple day number, without using system libraries (for example, in embedded code that doesn't have those librarie...")
 
 
Line 1: Line 1:
Sometimes you want to convert a gregorian year-month-day to a simple day number, without using system libraries (for example, in embedded code that doesn't have those libraries).
+
Sometimes you want to convert a [https://en.wikipedia.org/wiki/Gregorian_calendar Gregorian] year-month-day to a simple day number, without using system libraries (for example, in embedded code that doesn't have those libraries).
The following python onliner does that (for days after 1900-02-28 and before 2100-03-01):
+
The following python-3 onliner does that (for days after 1900-02-28 and before 2100-03-01):
   
 
<nowiki>
 
<nowiki>

Latest revision as of 21:20, 20 February 2022

Sometimes you want to convert a Gregorian year-month-day to a simple day number, without using system libraries (for example, in embedded code that doesn't have those libraries). The following python-3 onliner does that (for days after 1900-02-28 and before 2100-03-01):

def calcday(y, m, d):
    """
    Return days since 2000-01-01
    y: year
    m: month (1=jan)
    d: day (1=first day of month)
    """ 
    return m*30 + 14*m//25+d - (m>2)*(2-(y%4==0))+365*y+(y-1)//4 - 730530