Comments date notation
by JibstaMan
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<div id="today"></div>
<div id="given"></div>
<div id="format"></div>
<div id="output3"></div>
<div id="output4"></div>
<div id="output5"></div>
<div id="output6"></div>
<div id="output7"></div>
<div id="output8"></div>
<div id="output9"></div>
<div id="output10"></div>
CSS
body {
font-family:"Lucide Console", monospace;
}
JavaScript
// note UTC time 22:00 = 00:00 W. Europe Summer
var FormatDate = (function() {
return function(date, format)
{
if (typeof date === 'string' || typeof date === 'number')
{
date = new Date(date);
}
if (isNaN(date.valueOf()))
{
console.error("Couldn't parse date because of a bad argument.");
return "";
}
if (typeof format === 'undefined')
{
var now = new Date();
var posix = new Date(date).setHours(0, 0, 0, 0);
$('#given').text("Given: " + posix + " (" + new Date(posix) + ")");
var nowPosix = now.setHours(0, 0, 0, 0);
$('#today').text("Today: " + nowPosix + " (" + new Date(nowPosix) + ")");
var diff = nowPosix - posix;
if (diff === 0)
{
format = "HH:mm A";
}
else
{
var aDay = 1000 * 60 * 60 * 24;
$('#output3').text(diff + " (" + aDay + ")");
if (diff <= aDay)
{
format = "[Yesterday] - HH:mm A";
}
else
{
format = "ddd, DD MMMM YYYY - HH:mm A";
}
}
}
$('#format').text('Date format: "' + format + '"');
console.log('Format: ' + format);
console.log('Date : ' + date);
return moment(date).format(format);
};
})();
// create a day string relative to today
var testDate = new Date();
var testDateString = testDate.getFullYear() + "-";
var m = testDate.getMonth();
testDateString += ((m + 1) < 10) ? "0" + (m + 1) : (m + 1);
testDateString += "-";
var dates = [];
dates.push(testDate.setHours(8)); // setHours returns posix time
dates.push(new Date(testDate.setHours(1, 0, 0, 0)).toISOString()); // should be...