s-date
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -6,4 +6,5 @@ { | ||
"no-var": 0, | ||
"prefer-template": 0 | ||
"prefer-template": 0, | ||
"max-len": 0 | ||
} | ||
} |
201
index.js
'use strict'; | ||
// ----- Date formatter | ||
// -- @param format {String} | ||
// -- @param date {Date} optional date context | ||
// -- @return {String} | ||
// ----- years | ||
// --------------------------------------- | ||
module.exports = function formatDate(format, date) { | ||
function yyyy(date) { | ||
return date.getFullYear().toString(); // {yyyy} | ||
} | ||
// default to today | ||
if (!date) { | ||
date = new Date(); | ||
} | ||
function yy(date) { | ||
return date.getFullYear().toString().slice(-2); // {yy} | ||
} | ||
// ----- years | ||
// --------------------------------------- | ||
var yyyy = date.getFullYear().toString(); // {yyyy} | ||
var yy = yyyy.slice(-2); // {yy} | ||
// ----- months | ||
// --------------------------------------- | ||
// ----- months | ||
// --------------------------------------- | ||
function m(date) { | ||
var monthInt = date.getMonth() + 1; | ||
var m = monthInt.toString(); // {m} | ||
var mm = monthInt < 10 ? '0' + m : m; // {mm} | ||
return monthInt.toString(); // {m} | ||
} | ||
function mm(date) { | ||
var monthInt = date.getMonth() + 1; | ||
var mth = monthInt.toString(); | ||
return monthInt < 10 ? '0' + mth : mth; // {mm} | ||
} | ||
function month(date) { | ||
var months = [ | ||
@@ -39,14 +41,25 @@ 'January', | ||
]; | ||
return months[date.getMonth()]; // {Month} | ||
} | ||
var month = months[date.getMonth()]; // {Month} & {Mo} | ||
// relies on month | ||
function mo(date) { | ||
return month(date).substr(0, 3); | ||
} | ||
// ----- days | ||
// --------------------------------------- | ||
function day(date) { | ||
return date.getDate(); | ||
} | ||
// ----- days | ||
// --------------------------------------- | ||
var day = date.getDate(); | ||
var dd = day < 10 ? '0' + day : day; // {dd} | ||
function dd(date) { | ||
var d = date.getDate(); | ||
return d < 10 ? '0' + d : d; // {dd} | ||
} | ||
function dayWithSuffix(date) { | ||
// ----------------- suffixes ----------------- | ||
var daySuffixes = Object.create(null); // so for in loop works | ||
var d = date.getDate(); | ||
var daySuffixes = Object.create(null); | ||
daySuffixes.st = [1, 21, 31]; | ||
@@ -62,3 +75,3 @@ daySuffixes.nd = [2, 22]; | ||
for (suffix in daySuffixes) { | ||
if (daySuffixes[suffix].indexOf(day) > -1) { | ||
if (daySuffixes[suffix].indexOf(d) > -1) { | ||
suffixNotFound = false; | ||
@@ -74,5 +87,7 @@ break; | ||
var dayWithSuffix = day + suffix; // {D} | ||
// ----------------- end suffixes ----------------- | ||
return d + suffix; | ||
} | ||
function weekday(date) { | ||
var weekdays = [ | ||
@@ -87,45 +102,105 @@ 'Monday', | ||
]; | ||
return weekdays[date.getDay()]; | ||
} | ||
var weekday = weekdays[date.getDay()]; | ||
// relies on weekday | ||
function weekday3(date) { | ||
return weekday(date).substr(0, 3); | ||
} | ||
// relies on weekday | ||
function weekday2(date) { | ||
return weekday(date).substr(0, 2); | ||
} | ||
// ----- hours | ||
// --------------------------------------- | ||
var hours24 = date.getHours(); | ||
var hh24 = hours24 < 10 ? '0' + hours24 : hours24; | ||
var hours = hours24 % 12 === 0 ? 12 : hours24 % 12; // {h} | ||
var hh = hours < 10 ? '0' + hours : hours; | ||
var ampm = hours24 < 12 ? 'am' : 'pm'; // {ampm} | ||
// relies on weekday | ||
function weekday1(date) { | ||
return weekday(date)[0]; | ||
} | ||
// ----- minutes, seconds | ||
// --------------------------------------- | ||
// ----- hours | ||
// --------------------------------------- | ||
function hours24(date) { | ||
return date.getHours(); | ||
} | ||
function hh24(date) { | ||
var h = date.getHours(); | ||
return h < 10 ? '0' + h : h; | ||
} | ||
function hours(date) { | ||
var h = date.getHours(); | ||
return h % 12 === 0 ? 12 : h % 12; // {h} | ||
} | ||
// relies on `hours` function | ||
function hh(date) { | ||
var h = hours(date); | ||
return h < 10 ? '0' + h : h; | ||
} | ||
function ampm(date) { | ||
return date.getHours() < 12 ? 'am' : 'pm'; // {ampm} | ||
} | ||
function AMPM(date) { | ||
return date.getHours() < 12 ? 'AM' : 'PM'; // {ampm} | ||
} | ||
// ----- minutes, seconds | ||
// --------------------------------------- | ||
function minutes(date) { | ||
var min = date.getMinutes(); | ||
var minutes = min < 10 ? '0' + min : min; | ||
return min < 10 ? '0' + min : min; | ||
} | ||
function seconds(date) { | ||
var sec = date.getSeconds(); | ||
var seconds = sec < 10 ? '0' + sec : sec; | ||
return sec < 10 ? '0' + sec : sec; | ||
} | ||
// ----- Date formatter | ||
// -- @param format {String} | ||
// -- @param date {Date} optional date context | ||
// -- @return {String} | ||
// --------------------------------------- | ||
module.exports = function formatDate(format, date) { | ||
// default to today | ||
if (!date) { | ||
date = new Date(); | ||
} | ||
var map = { | ||
'{yyyy}': yyyy, | ||
'{yy}': yy, | ||
'{m}': m, | ||
'{mm}': mm, | ||
'{Month}': month, | ||
'{Mo}': mo, | ||
'{d}': day, | ||
'{dd}': dd, | ||
'{ds}': dayWithSuffix, | ||
'{Weekday}': weekday, | ||
'{Day}': weekday3, | ||
'{Dy}': weekday2, | ||
'{D}': weekday1, | ||
'{h24}': hours24, | ||
'{hh24}': hh24, | ||
'{h}': hours, | ||
'{hh}': hh, | ||
'{ampm}': ampm, | ||
'{AMPM}': AMPM, | ||
'{Minutes}': minutes, | ||
'{Seconds}': seconds | ||
}; | ||
const regex = /\{yyyy\}|\{yy\}|\{m\}|\{mm\}|\{Month\}|\{Mo\}|\{d\}|\{dd\}|\{ds\}|\{Weekday\}|\{Day\}|\{Dy\}|\{D\}|\{h24\}|\{hh24\}|\{h\}|\{hh\}|\{ampm\}|\{AMPM\}|\{Minutes\}|\{Seconds\}/g; | ||
return format | ||
.replace(/\{yyyy\}/g, yyyy) | ||
.replace(/\{yy\}/g, yy) | ||
.replace(/\{m\}/g, m) | ||
.replace(/\{mm\}/g, mm) | ||
.replace(/\{Month\}/g, month) | ||
.replace(/\{Mo\}/g, month.substr(0, 3)) | ||
.replace(/\{d\}/g, day) | ||
.replace(/\{dd\}/g, dd) | ||
.replace(/\{ds\}/g, dayWithSuffix) | ||
.replace(/\{Weekday\}/g, weekday) | ||
.replace(/\{Day\}/g, weekday.substr(0, 3)) | ||
.replace(/\{Dy\}/g, weekday.substr(0, 2)) | ||
.replace(/\{D\}/g, weekday[0]) | ||
.replace(/\{h24\}/g, hours24) | ||
.replace(/\{hh24\}/g, hh24) | ||
.replace(/\{h\}/g, hours) | ||
.replace(/\{hh\}/g, hh) | ||
.replace(/\{ampm\}/g, ampm) | ||
.replace(/\{AMPM\}/g, ampm.toUpperCase()) | ||
.replace(/\{Minutes\}/g, minutes) | ||
.replace(/\{Seconds\}/g, seconds); | ||
.replace(regex, function(match) { | ||
return map[match](date); | ||
}); | ||
}; |
{ | ||
"name": "s-date", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Tiny date/time formatter", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
20288
407