Skip to content

Instantly share code, notes, and snippets.

@jvaz11
Created March 17, 2016 19:12
Show Gist options
  • Save jvaz11/afcf592b59583231f5a4 to your computer and use it in GitHub Desktop.
Save jvaz11/afcf592b59583231f5a4 to your computer and use it in GitHub Desktop.
/* How to use this snippet */
// - create a plain-text snippet containing the entire moment.js library (copy/paste it from http://momentjs.com/downloads/moment.js)
// - set the abbreviation for this snippet to something like "momentjslibrary"
// - create a JavaScript snippet and paste the code below into it. set the abbreviation to whatever you'd like
// **********************************************************************
// copy/paste everything below this comment ↓
// reference to the plain-text snippet with an abbreviation set to "momentjslibrary". This is TextExpander's way of importing a module
%snippet:momentjslibrary%
// TextExpander option to show a cleaner popup window (not required)
%filltop%
// assigns user's input to the value of the timeStampInput variable. quotes make it a string
var timeStampInput = "%filltext:name=timestamp%";
var timeAgoString, // "6 days ago"
dateObject;
// admittedly hacky check for type of timestamp. if it has a colon, it assumes it's an ISO 8601 string. if the input doesn't include a colon, it assumes it's a UNIX timestamp and converts the input into a string before creating a JavaScript Date object
if (timeStampInput.includes(':')) {
dateObject = new Date(timeStampInput);
timeAgoString = moment(dateObject).fromNow();
} else {
dateObject = new Date(parseInt(timeStampInput));
timeAgoString = moment(dateObject).fromNow();
}
// concatenated string. customize this!
var output = "about " + timeAgoString + " on " + moment(dateObject).format('dddd') + " " + moment(dateObject).format('LL') + " at " + moment(dateObject).format('LT') + ".";
// outputs expansion
TextExpander.appendOutput(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment