Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bitty/format-date

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bitty/format-date - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

86

dist/format-date.esm.js
/**
* Formatters are based on moment tokens. They receives a
* Date and returns it's format.
* A dictionary that has tokens and their corresponding formatter function.
*/
var formatters = {
DD: function (date) { return zero(date.getDate(), 2); },
D: function (date) { return date.getDate() + ''; },
MM: function (date) { return zero(date.getMonth() + 1, 2); },
M: function (date) { return date.getMonth() + 1 + ''; },
YYYY: function (date) { return zero(date.getFullYear(), 4); },
YY: function (date) { return (date.getFullYear() + '').substr(-2, 2); },
HH: function (date) { return zero(date.getHours(), 2); },
H: function (date) { return date.getHours() + ''; },
mm: function (date) { return zero(date.getMinutes(), 2); },
m: function (date) { return date.getMinutes() + ''; },
ss: function (date) { return zero(date.getSeconds(), 2); },
s: function (date) { return date.getSeconds() + ''; },
// prettier-ignore
var FORMATTERS = {
'DD': function (date) { return addZeroPads(2, '' + date.getDate()); },
'D': function (date) { return '' + date.getDate(); },
'MM': function (date) { return addZeroPads(2, '' + (date.getMonth() + 1)); },
'M': function (date) { return '' + (date.getMonth() + 1); },
'YYYY': function (date) { return addZeroPads(4, '' + date.getFullYear()); },
'YY': function (date) { return ('' + date.getFullYear()).substr(-2); },
'HH': function (date) { return addZeroPads(2, '' + date.getHours()); },
'H': function (date) { return '' + date.getHours(); },
'mm': function (date) { return addZeroPads(2, '' + date.getMinutes()); },
'm': function (date) { return '' + date.getMinutes(); },
'ss': function (date) { return addZeroPads(2, '' + date.getSeconds()); },
's': function (date) { return '' + date.getSeconds(); },
};
/**
* Add '0' pads to number value.
* Part of the matcher, a `RegExp`, for escaping texts.
*/
function zero(value, length) {
var string = value + '';
while (string.length < length)
string = '0' + string;
return string;
}
var ESCAPE = '\\[[^\\[\\]]*\\]';
/**
* Creates a matcher using formatters tokens and escape strategy.
* Creates the matcher using formatters' tokens and the escaping strategy.
* @returns {RegExp}
*/
function createMatcher() {
var ESCAPE = '\\[[^\\[\\]]*\\]';
var matchers = Object.keys(formatters).concat(ESCAPE);
var matchers = Object.keys(FORMATTERS).concat(ESCAPE);
return new RegExp(matchers.join('|'), 'g');
}
var matcher = createMatcher();
/**
* It replaces format tokens for corresponding Date formats.
* @example ```js
* format(new Date(), 'DD/MM/YYYY hh:mm:ss')
* ```
* @param date A Date instace.
* @param format A string with tokens based on moment.
* Add zero (`0`) pads to text's length match defined length.
* @param {Number} length - Expected length of text with zero pads.
* @param {String} text - Text that receives the zero pads if below length.
* @returns {String}
*/
function format(date, format) {
return format.replace(matcher, function (token) {
if (formatters.hasOwnProperty(token))
return formatters[token](date);
function addZeroPads(length, text) {
if (text.length >= length)
return text;
return addZeroPads(length, '0' + text);
}
/**
* Receives a `Date` and a format (with tokens based on Moment.js) in `string`
* and returns the same format replacing the tokens for values from `Date`.
* @example
* formatDate(new Date(), "DD/MM/YYYY hh:mm:ss");
* //=> "25/06/2020 11:59:28"
*
* formatDate(new Date(), "[Day] D [at] h'mm");
* //=> "Day 25 at 11'59"
* @param {Date} date - A `Date` instance.
* @param {String} format - A string with tokens (like Moment.js tokens).
* @returns {String}
*/
function formatDate(date, format) {
return format.replace(createMatcher(), function (token) {
if (FORMATTERS.hasOwnProperty(token))
return FORMATTERS[token](date);
return token.replace(/\[|\]/g, '');

@@ -55,3 +63,3 @@ });

export default format;
export default formatDate;
//# sourceMappingURL=format-date.esm.js.map

@@ -6,51 +6,59 @@ 'use strict';

/**
* Formatters are based on moment tokens. They receives a
* Date and returns it's format.
* A dictionary that has tokens and their corresponding formatter function.
*/
var formatters = {
DD: function (date) { return zero(date.getDate(), 2); },
D: function (date) { return date.getDate() + ''; },
MM: function (date) { return zero(date.getMonth() + 1, 2); },
M: function (date) { return date.getMonth() + 1 + ''; },
YYYY: function (date) { return zero(date.getFullYear(), 4); },
YY: function (date) { return (date.getFullYear() + '').substr(-2, 2); },
HH: function (date) { return zero(date.getHours(), 2); },
H: function (date) { return date.getHours() + ''; },
mm: function (date) { return zero(date.getMinutes(), 2); },
m: function (date) { return date.getMinutes() + ''; },
ss: function (date) { return zero(date.getSeconds(), 2); },
s: function (date) { return date.getSeconds() + ''; },
// prettier-ignore
var FORMATTERS = {
'DD': function (date) { return addZeroPads(2, '' + date.getDate()); },
'D': function (date) { return '' + date.getDate(); },
'MM': function (date) { return addZeroPads(2, '' + (date.getMonth() + 1)); },
'M': function (date) { return '' + (date.getMonth() + 1); },
'YYYY': function (date) { return addZeroPads(4, '' + date.getFullYear()); },
'YY': function (date) { return ('' + date.getFullYear()).substr(-2); },
'HH': function (date) { return addZeroPads(2, '' + date.getHours()); },
'H': function (date) { return '' + date.getHours(); },
'mm': function (date) { return addZeroPads(2, '' + date.getMinutes()); },
'm': function (date) { return '' + date.getMinutes(); },
'ss': function (date) { return addZeroPads(2, '' + date.getSeconds()); },
's': function (date) { return '' + date.getSeconds(); },
};
/**
* Add '0' pads to number value.
* Part of the matcher, a `RegExp`, for escaping texts.
*/
function zero(value, length) {
var string = value + '';
while (string.length < length)
string = '0' + string;
return string;
}
var ESCAPE = '\\[[^\\[\\]]*\\]';
/**
* Creates a matcher using formatters tokens and escape strategy.
* Creates the matcher using formatters' tokens and the escaping strategy.
* @returns {RegExp}
*/
function createMatcher() {
var ESCAPE = '\\[[^\\[\\]]*\\]';
var matchers = Object.keys(formatters).concat(ESCAPE);
var matchers = Object.keys(FORMATTERS).concat(ESCAPE);
return new RegExp(matchers.join('|'), 'g');
}
var matcher = createMatcher();
/**
* It replaces format tokens for corresponding Date formats.
* @example ```js
* format(new Date(), 'DD/MM/YYYY hh:mm:ss')
* ```
* @param date A Date instace.
* @param format A string with tokens based on moment.
* Add zero (`0`) pads to text's length match defined length.
* @param {Number} length - Expected length of text with zero pads.
* @param {String} text - Text that receives the zero pads if below length.
* @returns {String}
*/
function format(date, format) {
return format.replace(matcher, function (token) {
if (formatters.hasOwnProperty(token))
return formatters[token](date);
function addZeroPads(length, text) {
if (text.length >= length)
return text;
return addZeroPads(length, '0' + text);
}
/**
* Receives a `Date` and a format (with tokens based on Moment.js) in `string`
* and returns the same format replacing the tokens for values from `Date`.
* @example
* formatDate(new Date(), "DD/MM/YYYY hh:mm:ss");
* //=> "25/06/2020 11:59:28"
*
* formatDate(new Date(), "[Day] D [at] h'mm");
* //=> "Day 25 at 11'59"
* @param {Date} date - A `Date` instance.
* @param {String} format - A string with tokens (like Moment.js tokens).
* @returns {String}
*/
function formatDate(date, format) {
return format.replace(createMatcher(), function (token) {
if (FORMATTERS.hasOwnProperty(token))
return FORMATTERS[token](date);
return token.replace(/\[|\]/g, '');

@@ -60,3 +68,3 @@ });

exports.default = format;
exports.default = formatDate;
//# sourceMappingURL=format-date.js.map

@@ -8,51 +8,59 @@ (function (global, factory) {

/**
* Formatters are based on moment tokens. They receives a
* Date and returns it's format.
* A dictionary that has tokens and their corresponding formatter function.
*/
var formatters = {
DD: function (date) { return zero(date.getDate(), 2); },
D: function (date) { return date.getDate() + ''; },
MM: function (date) { return zero(date.getMonth() + 1, 2); },
M: function (date) { return date.getMonth() + 1 + ''; },
YYYY: function (date) { return zero(date.getFullYear(), 4); },
YY: function (date) { return (date.getFullYear() + '').substr(-2, 2); },
HH: function (date) { return zero(date.getHours(), 2); },
H: function (date) { return date.getHours() + ''; },
mm: function (date) { return zero(date.getMinutes(), 2); },
m: function (date) { return date.getMinutes() + ''; },
ss: function (date) { return zero(date.getSeconds(), 2); },
s: function (date) { return date.getSeconds() + ''; },
// prettier-ignore
var FORMATTERS = {
'DD': function (date) { return addZeroPads(2, '' + date.getDate()); },
'D': function (date) { return '' + date.getDate(); },
'MM': function (date) { return addZeroPads(2, '' + (date.getMonth() + 1)); },
'M': function (date) { return '' + (date.getMonth() + 1); },
'YYYY': function (date) { return addZeroPads(4, '' + date.getFullYear()); },
'YY': function (date) { return ('' + date.getFullYear()).substr(-2); },
'HH': function (date) { return addZeroPads(2, '' + date.getHours()); },
'H': function (date) { return '' + date.getHours(); },
'mm': function (date) { return addZeroPads(2, '' + date.getMinutes()); },
'm': function (date) { return '' + date.getMinutes(); },
'ss': function (date) { return addZeroPads(2, '' + date.getSeconds()); },
's': function (date) { return '' + date.getSeconds(); },
};
/**
* Add '0' pads to number value.
* Part of the matcher, a `RegExp`, for escaping texts.
*/
function zero(value, length) {
var string = value + '';
while (string.length < length)
string = '0' + string;
return string;
}
var ESCAPE = '\\[[^\\[\\]]*\\]';
/**
* Creates a matcher using formatters tokens and escape strategy.
* Creates the matcher using formatters' tokens and the escaping strategy.
* @returns {RegExp}
*/
function createMatcher() {
var ESCAPE = '\\[[^\\[\\]]*\\]';
var matchers = Object.keys(formatters).concat(ESCAPE);
var matchers = Object.keys(FORMATTERS).concat(ESCAPE);
return new RegExp(matchers.join('|'), 'g');
}
var matcher = createMatcher();
/**
* It replaces format tokens for corresponding Date formats.
* @example ```js
* format(new Date(), 'DD/MM/YYYY hh:mm:ss')
* ```
* @param date A Date instace.
* @param format A string with tokens based on moment.
* Add zero (`0`) pads to text's length match defined length.
* @param {Number} length - Expected length of text with zero pads.
* @param {String} text - Text that receives the zero pads if below length.
* @returns {String}
*/
function format(date, format) {
return format.replace(matcher, function (token) {
if (formatters.hasOwnProperty(token))
return formatters[token](date);
function addZeroPads(length, text) {
if (text.length >= length)
return text;
return addZeroPads(length, '0' + text);
}
/**
* Receives a `Date` and a format (with tokens based on Moment.js) in `string`
* and returns the same format replacing the tokens for values from `Date`.
* @example
* formatDate(new Date(), "DD/MM/YYYY hh:mm:ss");
* //=> "25/06/2020 11:59:28"
*
* formatDate(new Date(), "[Day] D [at] h'mm");
* //=> "Day 25 at 11'59"
* @param {Date} date - A `Date` instance.
* @param {String} format - A string with tokens (like Moment.js tokens).
* @returns {String}
*/
function formatDate(date, format) {
return format.replace(createMatcher(), function (token) {
if (FORMATTERS.hasOwnProperty(token))
return FORMATTERS[token](date);
return token.replace(/\[|\]/g, '');

@@ -62,3 +70,3 @@ });

exports.default = format;
exports.default = formatDate;

@@ -65,0 +73,0 @@ Object.defineProperty(exports, '__esModule', { value: true });

@@ -1,2 +0,2 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).formatDate={})}(this,(function(e){"use strict";var t={DD:function(e){return n(e.getDate(),2)},D:function(e){return e.getDate()+""},MM:function(e){return n(e.getMonth()+1,2)},M:function(e){return e.getMonth()+1+""},YYYY:function(e){return n(e.getFullYear(),4)},YY:function(e){return(e.getFullYear()+"").substr(-2,2)},HH:function(e){return n(e.getHours(),2)},H:function(e){return e.getHours()+""},mm:function(e){return n(e.getMinutes(),2)},m:function(e){return e.getMinutes()+""},ss:function(e){return n(e.getSeconds(),2)},s:function(e){return e.getSeconds()+""}};function n(e,t){for(var n=e+"";n.length<t;)n="0"+n;return n}var r,u=(r=Object.keys(t).concat("\\[[^\\[\\]]*\\]"),new RegExp(r.join("|"),"g"));e.default=function(e,n){return n.replace(u,(function(n){return t.hasOwnProperty(n)?t[n](e):n.replace(/\[|\]/g,"")}))},Object.defineProperty(e,"__esModule",{value:!0})}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).formatDate={})}(this,(function(e){"use strict";var t={DD:function(e){return n(2,""+e.getDate())},D:function(e){return""+e.getDate()},MM:function(e){return n(2,""+(e.getMonth()+1))},M:function(e){return""+(e.getMonth()+1)},YYYY:function(e){return n(4,""+e.getFullYear())},YY:function(e){return(""+e.getFullYear()).substr(-2)},HH:function(e){return n(2,""+e.getHours())},H:function(e){return""+e.getHours()},mm:function(e){return n(2,""+e.getMinutes())},m:function(e){return""+e.getMinutes()},ss:function(e){return n(2,""+e.getSeconds())},s:function(e){return""+e.getSeconds()}};function n(e,t){return t.length>=e?t:n(e,"0"+t)}e.default=function(e,n){return n.replace((r=Object.keys(t).concat("\\[[^\\[\\]]*\\]"),new RegExp(r.join("|"),"g")),(function(n){return t.hasOwnProperty(n)?t[n](e):n.replace(/\[|\]/g,"")}));var r},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=format-date.umd.min.js.map
{
"name": "@bitty/format-date",
"version": "1.0.0",
"description": "A small library (around 400 B when gziped & minified) to format JavaScript `Date` object using same tokens as moment.",
"cdn": "dist/format-date.umd.js",
"main": "dist/format-date.js",
"types": "types/format-date.d.ts",
"unpkg": "dist/format-date.umd.js",
"module": "dist/format-date.esm.js",
"jsdelivr": "dist/format-date.umd.js",
"umd:main": "dist/format-date.umd.js",
"files": [
"types/",
"dist/"
],
"scripts": {
"lint": "prettier --check \"./src/**/*.ts\"",
"lint:fix": "prettier --write \"./src/**/*.ts\"",
"build": "pnpm run build:transpile && pnpm run build:bundle",
"build:transpile": "tsc --project ./tsconfig.build.json",
"build:bundle": "rollup --config ./rollup.config.js",
"test": "pnpm run lint && pnpm run test:transpile && ava",
"test:transpile": "tsc --project ./tsconfig.test.json",
"prepublishOnly": "pnpm run test && pnpm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/VitorLuizC/format-date.git"
},
"author": {
"url": "https://vitorluizc.github.io/",
"name": "Vitor Luiz Cavalcanti",
"email": "vitorluizc@outlook.com"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/VitorLuizC/format-date/issues"
},
"homepage": "https://github.com/VitorLuizC/format-date",
"devDependencies": {
"ava": "^3.9.0",
"prettier": "^2.0.5",
"rollup": "^2.16.1",
"rollup-plugin-terser": "^6.1.0",
"typescript": "^3.9.5"
}
"name": "@bitty/format-date",
"version": "1.1.0",
"description": "A small library (around 400 B when gziped & minified) to format JavaScript `Date` object using same tokens as moment.",
"cdn": "dist/format-date.umd.js",
"main": "dist/format-date.js",
"types": "types/format-date.d.ts",
"unpkg": "dist/format-date.umd.js",
"module": "dist/format-date.esm.js",
"jsdelivr": "dist/format-date.umd.js",
"umd:main": "dist/format-date.umd.js",
"files": [
"types/",
"dist/"
],
"scripts": {
"lint": "prettier --check \"./src/**/*.ts\"",
"lint:fix": "prettier --write \"./src/**/*.ts\"",
"build": "pnpm run build:transpile && pnpm run build:bundle",
"build:transpile": "tsc --project ./tsconfig.build.json",
"build:bundle": "rollup --config ./rollup.config.js",
"test": "pnpm run lint && pnpm run test:transpile && ava",
"test:transpile": "tsc --project ./tsconfig.test.json",
"prepublishOnly": "pnpm run test && pnpm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/VitorLuizC/format-date.git"
},
"author": {
"url": "https://vitorluizc.github.io/",
"name": "Vitor Luiz Cavalcanti",
"email": "vitorluizc@outlook.com"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/VitorLuizC/format-date/issues"
},
"homepage": "https://github.com/VitorLuizC/format-date",
"devDependencies": {
"ava": "^3.9.0",
"prettier": "^2.0.5",
"rollup": "^2.18.1",
"rollup-plugin-terser": "^6.1.0",
"typescript": "^3.9.5"
}
}

@@ -11,3 +11,3 @@ # `@bitty/format-date`

- ⚡ Lightweight:
- Weighs less than 0.4KB (min + gzip).
- Weighs around 0.4KB (min + gzip).

@@ -62,25 +62,25 @@ - 🔋 Bateries included:

formatDate(new Date(), 'DD/MM/YYYY HH [hours] and mm [minutes].');
// I'm escaping "hours" and "minutes" using same syntax as moment.
formatDate(new Date(), 'DD/MM/YYYY HH [hours] [and] mm [minutes].');
// I'm escaping "hours", "and" and "minutes" using same syntax as Moment.js.
```
## Supported tokens
## Tokens
Right now this lib supports the tokens below.
| Token | Type | Range |
| ----- | ------ | ------------ |
| DD | Day | 01 to 31 |
| D | Day | 1 to 31 |
| MM | Month | 01 to 12 |
| M | Month | 1 to 12 |
| YYYY | Year | 0000 to 9999 |
| YY | Year | 0 to 99 |
| HH | Hour | 00 to 23 |
| H | Hour | 0 to 23 |
| mm | Minute | 00 to 59 |
| m | Minute | 0 to 59 |
| ss | Second | 00 to 59 |
| s | Second | 0 to 59 |
| [*] | Escape | &nbsp; |
| &nbsp; | Token | Output |
| -----------: | :----- | :---------------------------------- |
| Year | `YY` | `70`, `71`, ... `19`, `20`. |
| | `YYYY` | `1970`, `1971`, ... `2019`, `2020`. |
| Month | `M` | `1`, `2`, ... `11`, `12`. |
| | `MM` | `01`, `02`, ... `11`, `12`. |
| Day of Month | `D` | `1`, `2`, ... `30`, `31`. |
| | `DD` | `01`, `02`, ... `30`, `31`. |
| Hour | `H` | `1`, `2`, ... , `22`,`23`. |
| | `HH` | `01`, `02`, ... , `22`,`23`. |
| Minute | `m` | `1`, `2`, ... , `58`,`59`. |
| | `mm` | `01`, `02`, ... , `58`,`59`. |
| Second | `s` | `1`, `2`, ... , `58`,`59`. |
| | `ss` | `01`, `02`, ... , `58`,`59`. |
| Escape | `[*]` | &nbsp; |

@@ -87,0 +87,0 @@ ## License

/**
* It replaces format tokens for corresponding Date formats.
* @example ```js
* format(new Date(), 'DD/MM/YYYY hh:mm:ss')
* ```
* @param date A Date instace.
* @param format A string with tokens based on moment.
* Receives a `Date` and a format (with tokens based on Moment.js) in `string`
* and returns the same format replacing the tokens for values from `Date`.
* @example
* formatDate(new Date(), "DD/MM/YYYY hh:mm:ss");
* //=> "25/06/2020 11:59:28"
*
* formatDate(new Date(), "[Day] D [at] h'mm");
* //=> "Day 25 at 11'59"
* @param {Date} date - A `Date` instance.
* @param {String} format - A string with tokens (like Moment.js tokens).
* @returns {String}
*/
declare function format(date: Date, format: string): string;
export default format;
export default function formatDate(date: Date, format: string): string;
//# sourceMappingURL=format-date.d.ts.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc