JSFiddle - React, Tailwind, and code Playground
by dougneiner
HTML
<span>UPDATED:</span> <script>renderTimestamp( "2012-07-18 00:20:00 GMT-400" )</script>
JavaScript
function renderTimestamp( date ) {
var dateParts = date.split( /[ \-:]/g ),
hour = dateParts[ 3 ],
minutes = dateParts[ 4 ],
ampm = "AM",
difference = (+new Date()) - Date.parse(date);
difference = Math.round( difference / ( 1000 * 60 ) ); // Get minutes
if ( difference < 15 ) {
document.write( difference + " minutes ago" );
} else {
if ( hour >= 12 ) {
hour = hour - 12;
ampm = "PM";
}
document.write(hour + ":" + minutes + ampm + " ET");
}
}