Skip to content

Commit

Permalink
feat: add milliseconds support;
Browse files Browse the repository at this point in the history
- Closes #5
  • Loading branch information
lukeed committed Jun 4, 2019
1 parent 08349b7 commit 22ada81
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var dict = {
DD: 'getDate',
HH: 'getHours',
mm: 'getMinutes',
ss: 'getSeconds'
ss: 'getSeconds',
fff: 'getMilliseconds'
};

export default function (str) {
Expand Down
8 changes: 6 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('tape');
const fn = require('../dist/tinydate');

const foo = new Date('5/1/2017, 4:30:09 PM');
const render = str => fn(str)(foo);
const render = (str, ctx) => fn(str)(ctx || foo);

test('tinydate', t => {
t.equal(typeof fn, 'function', 'exports a function');
Expand All @@ -24,9 +24,13 @@ test('rendering', t => {
t.equal(render('{HH}'), '16', 'returns full hours (24h)');
t.equal(render('{mm}'), '30', 'returns padded minutes');
t.equal(render('{ss}'), '09', 'returns seconds');

t.equal(render('{fff}'), '000', 'returns milliseconds (default 000)');
t.equal(render('{fff}', new Date(1559607289771)), '771', 'returns milliseconds');

// formats
t.equal(render('[{HH}:{mm}:{ss}]'), '[16:30:09]', 'returns formatted time string');
t.equal(render('The date is {MM}/{DD}/{YYYY}!'), 'The date is 05/01/2017!', 'returns formatted date string');
t.equal(render('Created on: [{YYYY}-{MM}-{DD} ~ {HH}:{mm}:{ss}]'), 'Created on: [2017-05-01 ~ 16:30:09]', 'kitchen sink');
t.equal(render('Created on: [{YYYY}-{MM}-{DD} ~ {HH}:{mm}:{ss}.{fff}]'), 'Created on: [2017-05-01 ~ 16:30:09.000]', 'kitchen sink');
t.end();
});

0 comments on commit 22ada81

Please sign in to comment.