Last active
June 7, 2016 17:44
-
-
Save sisiwei/44dc79ae23be45c0906d to your computer and use it in GitHub Desktop.
Converting Dates to AP Style, using Javascript
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
// Adapated from: https://github.com/banterability/dateline | |
var APDate = function(dateObj){ | |
this.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | |
this.monthNames = ['Jan.', 'Feb.', 'March', 'April', 'May', 'June', 'July', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.']; | |
return this.Deadline(dateObj); | |
}; | |
APDate.prototype.Deadline = function(dateObj) { | |
var that = this; | |
if (dateObj == null) { | |
dateObj = new Date(); | |
} | |
dateObj.getAPTime = function(options) { | |
var hour, hours, minute, minutes, timeOfDay; | |
if (options == null) { | |
options = {}; | |
} | |
hours = dateObj.getHours(); | |
minutes = dateObj.getMinutes(); | |
if (hours === 0 && minutes === 0) { | |
return 'midnight'; | |
} | |
if (hours === 12 && minutes === 0) { | |
return 'noon'; | |
} | |
timeOfDay = hours < 12 ? 'a.m.' : 'p.m.'; | |
hour = that.formatHours(hours); | |
if (that.showMinutes(minutes, options)) { | |
return "" + hour + " " + timeOfDay; | |
} | |
minute = that.formatMinutes(minutes); | |
return "" + hour + ":" + minute + " " + timeOfDay; | |
}; | |
dateObj.getAPDate = function(options) { | |
var date, month, monthName, year; | |
if (options == null) { | |
options = {}; | |
} | |
month = dateObj.getUTCMonth(); | |
date = dateObj.getUTCDate(); | |
year = dateObj.getUTCFullYear(); | |
monthName = that.monthNames[month]; | |
if (that.useDayName(dateObj, options)) { | |
return that.getDayOfWeek(dateObj); | |
} else if (that.showYear(year, options)) { | |
return "" + monthName + " " + date; | |
} else { | |
return "" + monthName + " " + date + ", " + year; | |
} | |
}; | |
return dateObj; | |
}; | |
APDate.prototype.formatHours = function(hours) { | |
if (hours === 0) { | |
return 12; | |
} | |
if (hours > 12) { | |
return hours - 12; | |
} | |
return hours; | |
}; | |
APDate.prototype.topOfHour = function(minutes) { | |
return minutes === 0; | |
}; | |
APDate.prototype.formatMinutes = function(minutes) { | |
if (minutes < 10) { | |
return "0" + minutes; | |
} | |
return minutes; | |
}; | |
APDate.prototype.showMinutes = function(minutes, options) { | |
return this.topOfHour(minutes) && (options.includeMinutes == null); | |
}; | |
APDate.prototype.showYear = function(year, options) { | |
return (year === new Date().getFullYear()) && (options.includeYear == null); | |
}; | |
APDate.prototype.getDayOfWeek = function(dateObj) { | |
return this.dayNames[dateObj.getDay()]; | |
}; | |
APDate.prototype.useDayName = function(dateObj, options) { | |
return (options.useDayNameForLastWeek != null) && this.withinSevenDays(new Date(), dateObj); | |
}; | |
APDate.prototype.withinSevenDays = function(date1, date2) { | |
var oneDayInMs, _ref; | |
oneDayInMs = 1000 * 60 * 60 * 24; | |
return (-7 < (_ref = (date2 - date1) / oneDayInMs) && _ref < 0); | |
}; | |
// A specifc date | |
// new APDate(new Date('2014-02-17')).getAPDate(); | |
// Today | |
// new APDate().getAPDate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment