Skip to content

Instantly share code, notes, and snippets.

@bmschmidt
Created July 31, 2014 19:49
Show Gist options
  • Save bmschmidt/4ac057575a322c32b1c6 to your computer and use it in GitHub Desktop.
Save bmschmidt/4ac057575a322c32b1c6 to your computer and use it in GitHub Desktop.
Date functions

MySQL has a canonical implementation:

SELECT FROM_DAYS(730669);

+-------------------+
| FROM_DAYS(730669) |
+-------------------+
| 2000-07-03        |
+-------------------+
1 row in set (0.01 sec)

Someone wrote a special javascript script:


getDate2 = function(intval) {
    var val = new(Date);
    val.setFullYear(0,0,intval+1)
    return val
}
    getDate = function(intval) {
      var minDate;
      minDate = new Date();
      minDate.setUTCFullYear(0);
      minDate.setUTCMonth(0);
      minDate.setUTCDate(1);
      minDate.setUTCSeconds(0);
      minDate.setUTCMilliseconds(0);
      minDate.setDate(minDate.getDate() + intval);
      return minDate;
    };


getDate(730669)

//Mon Jul 03 2000 11:52:00 GMT-0400 (EDT)

Python we only use for creating:

from datetime import date, datetime

def DaysSinceZero(dateobj):
    #Zero isn't a date, which python knows but MySQL and javascript don't.
    return (dateobj - date(1,1,1)).days + 366

"""
In [20]: DaysSinceZero(date(2000,07,03))
Out[20]: 730669

"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment