Created
February 20, 2015 00:58
-
-
Save irisli/3129ab5603abe5faf07b to your computer and use it in GitHub Desktop.
Twitter Date Format
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
// This formats dates like how Twitter does on their tweets I originally wrote | |
// this because I did not want to add an external dependency (such as moment) to | |
// my project for something so insignificant (date format). I couldn't have | |
// easily generated it on the server side since the dates are relative to the | |
// end user's browser's timezone (and I specifically wrote this to run on the | |
// client). | |
// | |
// Usage: twitterDateFormat(time) | |
// time can be anything JS' Date can understand | |
var twitterDateFormat = (function() { | |
var months = [ | |
"Jan", | |
"Feb", | |
"Mar", | |
"Apr", | |
"May", | |
"Jun", | |
"Jul", | |
"Aug", | |
"Sep", | |
"Oct", | |
"Nov", | |
"Dec" | |
] | |
return function(time) { | |
d = new Date(time); | |
return (d.getHours() % 12 || 12) + ':' + String("00" + d.getMinutes()).slice(-2) + ' ' + (d.getHours() < 12 ? 'AM' : 'PM') + ' - ' + d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear(); | |
} | |
})() | |
/** | |
Testing: | |
I wrote a little test for this. It only outputs and rerquires a human to verify the tests' outcome | |
var d = new Date() | |
for (var i = 0; i < 49; i++) { | |
var d = new Date(d.getTime() + 1800000); | |
console.log(twitterDateFormat(d)) | |
} | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment