Skip to content

Instantly share code, notes, and snippets.

@kawanet
Last active November 26, 2022 01:52
Show Gist options
  • Save kawanet/4600716 to your computer and use it in GitHub Desktop.
Save kawanet/4600716 to your computer and use it in GitHub Desktop.
strftime - JavaScript implementation for strftime() function of ISO C90 + α
const en_US = {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
AM: "AM",
PM: "PM",
};
const literal = {
"%%": "%",
"%n": "\n",
"%t": "\t",
};
const gen = (locale) => {
return (format, dt) => {
return format.replace(/%(-)?([a-zA-Z%])/g, ($$, $1, $2) => {
let val = literal[$$];
if (val)
return val;
const pad = (len, num) => ($1) ? num : ("000000000" + num).substr(-len, len);
switch ($2) {
/**
* %a The abbreviated weekday name according to the current locale. (en-only)
*/
case "a":
return locale.shortDays[dt.getDay()];
/**
* %A The full weekday name according to the current locale. (en-only)
*/
case "A":
return locale.days[dt.getDay()];
/**
* %b The abbreviated month name according to the current locale. (en-only)
*/
case "b":
return locale.shortMonths[dt.getMonth()];
/**
* %B The full month name according to the current locale. (en-only)
*/
case "B":
return locale.months[dt.getMonth()];
/**
* %C The century number (year/100) as a 2-digit integer. (SU)
*/
case "C":
return pad(2, Math.floor(dt.getFullYear() / 100));
/**
* %c The preferred date and time representation for the current locale. [NOT IMPLEMENTED]
* %d The day of the month as a decimal number (range 01 to 31).
*/
case "d":
return pad(2, dt.getDate());
/**
* %H The hour as a decimal number using a 24-hour clock (range 00 to 23).
*/
case "H":
return pad(2, dt.getHours());
/**
* %I The hour as a decimal number using a 12-hour clock (range 01 to 12).
*/
case "I":
return pad(2, ((dt.getHours() + 11) % 12) + 1);
/**
* %L - Millisecond of the second (000..999)
*/
case "L":
$1 = null;
return pad(3, dt.getMilliseconds());
/**
* %m The month as a decimal number (range 01 to 12).
*/
case "m":
return pad(2, dt.getMonth() + 1);
/**
* %M The minute as a decimal number (range 00 to 59).
*/
case "M":
return pad(2, dt.getMinutes());
/**
* %p Either "AM" or "PM"
*/
case "p":
return dt.getHours() < 12 ? locale.AM : locale.PM;
/**
* %S The second as a decimal number (range 00 to 60)
*/
case "S":
return pad(2, dt.getSeconds());
/**
* %U The week number [NOT IMPLEMENTED]
* %y The year as a decimal number without a century (range 00 to 99).
*/
case "y":
return pad(2, dt.getFullYear() % 100);
/**
* %Y The year as a decimal number including the century.
*/
case "Y":
return pad(4, dt.getFullYear());
/**
* %z The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC). (SU)
*/
case "z":
val = -dt.getTimezoneOffset();
let hour = Math.floor(val / 60);
let min = val % 60;
if (hour < 10)
hour = "0" + hour;
if (min < 10)
min = "0" + min;
return (val < 0 ? "-" : "+") + hour + min;
default:
return $$;
}
});
};
};
exports.strftime = gen(en_US);
@kawanet
Copy link
Author

kawanet commented Jan 23, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment