Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Try to find birthdays by last/next n-days
DAYMS = 1000 * 60 * 60 * 24
checkBirthday = ( now, bd, diff )->
# differ between next and last days (look back or forward)
if diff > 0
start = new Date( now + " 00:00:00 +0000" )
end = new Date( start.valueOf() + ( DAYMS * diff ) )
year = new Date( end ).getFullYear()
else
end = new Date( now + " 00:00:00 +0000" )
start = new Date( end.valueOf() + ( DAYMS * diff ) )
year = new Date( start ).getFullYear()
# reset the year to be comparable
birthday = new Date( bd + " 00:00:00 +0000" ).setFullYear( year )
console.log( 'birthday', start, " : ", new Date( birthday ), " : ", end )
# check if the changed birthday is within the start and end date
if birthday >= start and birthday <= end
return true
else
return false
# Test the edge cases
# now, birthday, diff, check
cases = [
[ "A", "2017-01-02", "1985-01-02", 7, true ]
[ "B1", "2017-01-02", "1985-01-09", 7, true ]
[ "B1", "2017-01-01", "1985-01-09", 7, false ]
[ "B2", "2017-01-02", "1985-01-08", 7, true ]
[ "C", "2016-12-31", "1985-01-01", 7, true ]
[ "D", "2017-01-01", "1985-12-31", -7, true ]
[ "E1", "2016-02-22", "2000-02-29", 7, true ]
[ "E2", "2016-02-22", "2001-02-29", 7, false ]
[ "F", "2015-02-22", "2000-02-29", 7, true ]
[ "G", "2016-12-28", "1985-01-02", 7, true ]
[ "H", "2016-12-28", "1985-01-02", -7, false ]
[ "I", "2016-12-28", "1985-01-02", -7, false ]
[ "J", "2016-01-04", "1985-12-29", -7, true ]
[ "K", "2016-01-04", "1985-12-29", -7, true ]
]
for [ name, now, bd, diff, res ] in cases
if checkBirthday( now, bd, diff ) is res
console.log(name, "OK" )
else
console.log(name, "Failed" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment