Skip to content

Commit 122e22d

Browse files
author
Thomas Timmer
committed
feat: first commit
0 parents  commit 122e22d

File tree

5 files changed

+4902
-0
lines changed

5 files changed

+4902
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dayjs-plugin-strftime
2+
3+
Adding strftime to day js.
4+
5+
It is just a dayjs wrapper around [strftime](https://github.com/samsonjs/strftime). Go there about customization
6+
7+
## examples
8+
9+
```js
10+
const dayjs = require("dayjs");
11+
const strftime = require("dayjs-plugin-strftime");
12+
13+
dayjs.extend(strftime);
14+
15+
// this can be any dayjs object
16+
let now = dayjs();
17+
18+
// 2021-06-07 12:01:45
19+
now.strftime("%F %T");
20+
```
21+
22+
localization is done by:
23+
24+
```js
25+
const dayjs = require("dayjs");
26+
const strftime = require("dayjs-plugin-strftime");
27+
28+
dayjs.extend(strftime);
29+
30+
let now = dayjs();
31+
32+
// 25 januari, 2019 00:00:00
33+
now.strftime("%d %B, %Y %H:%M:%S", "nl_NL");
34+
```

__tests__/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const dayjs = require('dayjs');
2+
const strftime = require('../index.js');
3+
4+
dayjs.extend(strftime);
5+
6+
test('works', () => {
7+
expect(dayjs('2019-01-25').strftime("%B %d, %Y %H:%M:%S")).toBe("January 25, 2019 00:00:00");
8+
})
9+
10+
test('works localization', () => {
11+
expect(dayjs('2019-01-25').strftime("%d %B, %Y %H:%M:%S", "nl_NL")).toBe("25 januari, 2019 00:00:00");
12+
})

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const strftime = require('strftime');
2+
3+
const strftimePlugin = (option, dayjsClass, dayjsFactory) => {
4+
dayjsClass.prototype.strftime = function(args, local) {
5+
let modifiedFormatter;
6+
if (local) {
7+
modifiedFormatter = strftime.localizeByIdentifier(local)
8+
} else {
9+
modifiedFormatter = strftime
10+
};
11+
12+
return modifiedFormatter(args, this.toDate())
13+
}
14+
}
15+
16+
module.exports = strftimePlugin;

0 commit comments

Comments
 (0)