Created
April 17, 2011 21:51
-
-
Save fbuchinger/924511 to your computer and use it in GitHub Desktop.
Mockup implementation of a DateTime Tagger. Requires underscore.js and doesn't yet follow the pictagger.api. Use with care
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dateTagger(date) { | |
var months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']; | |
var weekdays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; | |
function getWeekOfYear(date) { | |
a = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 5); | |
b = new Date(a.getFullYear(), 0, 4); | |
return ~~ ((a - b) / 864e5 / 7 + 1.5); | |
} | |
function getDaytime(date) { | |
var hours = date.getHours(); | |
var daytimes = ['night', 'morning', 'noon', 'afternoon', 'evening', 'night']; | |
var daytimeBegins = [6, 11, 13, 18, 21]; | |
return daytimes[_(daytimeBegins).sortedIndex(hours)]; | |
} | |
function getSeason(date) { | |
var dYear = date.getFullYear(); | |
var seasonStarts = [ | |
new Date(dYear, 2, 20), //SPRING | |
new Date(dYear, 5, 21), //SUMMER | |
new Date(dYear, 8, 22), //AUTUMN | |
new Date(dYear, 11, 21) //WINTER | |
]; | |
var seasons = ['winter', 'spring', 'summer', 'autumn', 'winter']; | |
return seasons[_(seasonStarts).sortedIndex(date)] | |
} | |
function zeropad(num) { | |
return (num < 10 ? '0' + num : num) | |
} | |
return [{ | |
category: 'year', | |
name: 'year' + date.getFullYear() | |
}, { | |
category: 'season', | |
name: getSeason(date) | |
}, { | |
category: 'month', | |
name: months[date.getMonth()] | |
}, { | |
category: 'daytime', | |
name: getDaytime(date) | |
}, { | |
category: 'calweek', | |
name: 'week' + zeropad(getWeekOfYear(date)) | |
}, { | |
category: 'weekday', | |
name: weekdays[date.getDay()] | |
}, { | |
category: 'monthday', | |
name: 'day' + zeropad(date.getDate()) | |
}] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment