Skip to content

Instantly share code, notes, and snippets.

@PlanBrewski
Created December 3, 2012 18:57
Show Gist options
  • Save PlanBrewski/4197107 to your computer and use it in GitHub Desktop.
Save PlanBrewski/4197107 to your computer and use it in GitHub Desktop.
moment.js - displayEventDate
<html>
<head>
<script src="https://raw.github.com/timrwood/moment/master/moment.js"></script>
<script type="text/javascript">
function displayEventDate(startDate, endDate, alertDate){
startDate = moment.unix(startDate);
endDate = moment.unix(endDate);
var theEventDate = null;
//Same Day?
if(startDate.diff(endDate, 'days') === 0){
theEventDate = startDate.format('MMMM Do, YYYY');
} else {
//Diff Day, Same Year?
if(startDate.format('YYYY') === endDate.format('YYYY')){
//Diff Day, Same Year, Same Month?
if(startDate.format('M') === endDate.format('M')){
theEventDate = startDate.format('MMMM Do') + ' - ' + endDate.format('Do, YYYY');
} else {
//Diff Day, Same Year, Diff Month
theEventDate = startDate.format('MMMM Do') + ' - ' + endDate.format('MMMM Do, YYYY');
}
} else {
//Diff Day, Diff Year
theEventDate = startDate.format('MMMM Do, YYYY') + ' - ' + endDate.format('MMMM Do, YYYY');
}
}
return theEventDate;
}
//displayEventDate('1318681876','1318681876'); // October 15th, 2011
//displayEventDate('1318681876','1318781876'); // October 15th - 16th, 2011
//displayEventDate('1320085943','1320172343'); // October 31st - November 1th, 2011
//displayEventDate('1325288163','1325547363'); // Dec 30, 2011 - Jan 2, 2012
</script>
</head>
<body>
<h1>
<script type="text/javascript">
document.write(displayEventDate('1318681876','1318781876'));
</script>
</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment