Skip to content

Instantly share code, notes, and snippets.

@alanthai
alanthai / ordinals.js
Created November 17, 2014 15:46
Adds ordinal suffix to an integer
// assume positive integers as parameters
function getDigit(n, d) {
return parseInt( n / Math.pow(10, d - 1), 10 ) % 10;
}
function nth(n) {
var d1 = getDigit(n, 1);
var d2 = getDigit(n, 2);
n = "" + n;
@alanthai
alanthai / times.js
Last active August 29, 2015 14:07
Useful date functions and statements
// midnight today
var today = new Date(new Date().setHours(0,0,0,0));
// midnight tomorrow
var tomorrow = new Date(new Date().setHours(24,0,0,0));
// date format: yyyy-mm-dd HH:MM:SS
function pad(n, width, z) {
z = z || '0';
n = n + '';
@alanthai
alanthai / colorConvert.js
Last active August 26, 2019 07:46
Converts color hex to decimal
function toHex(n: number) {
return ('0' + n.toString(16)).slice(-2);
}
// ES6
function hexToRGB(code) {
const [r, g, b] = code.replace('#', '')
.match(/../g).map((hex) => parseInt(hex, 16));
return {r, g, b};