Skip to content

Commit 0bb3204

Browse files
Merge pull request #149 from fboes/feature/44-isoDateTime
Adding `p` as timezone offset, e.g. `+02:00`
2 parents 47bdabb + e48d5d4 commit 0bb3204

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ dateFormat(now, "N");
9797
| `MM` | Minutes; leading zero for single-digit minutes. |
9898
| `N` | ISO 8601 numeric representation of the day of the week. |
9999
| `o` | GMT/UTC timezone offset, e.g. -0500 or +0230. |
100+
| `p` | GMT/UTC timezone offset, e.g. -05:00 or +02:30. |
100101
| `s` | Seconds; no leading zero for single-digit seconds. |
101102
| `ss` | Seconds; leading zero for single-digit seconds. |
102103
| `S` | The date's ordinal suffix (st, nd, rd, or th). Works well with `d`. |
@@ -126,7 +127,7 @@ dateFormat(now, "N");
126127
| `longTime` | `h:MM:ss TT Z` | 5:46:21 PM EST |
127128
| `isoDate` | `yyyy-mm-dd` | 2007-06-09 |
128129
| `isoTime` | `HH:MM:ss` | 17:46:21 |
129-
| `isoDateTime` | `yyyy-mm-dd'T'HH:MM:ss` | 2007-06-09T17:46:21 |
130+
| `isoDateTime` | `yyyy-mm-dd'T'HH:MM:sso` | 2007-06-09T17:46:21+0700 |
130131
| `isoUtcDateTime` | `UTC:yyyy-mm-dd'T'HH:MM:ss'Z'` | 2007-06-09T22:46:21Z |
131132

132133
### Localization

benchmark/previousDateFormat.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function _typeof(obj) {
3636
"use strict";
3737

3838
var dateFormat = (function () {
39-
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZWN]|"[^"]*"|'[^']*'/g;
39+
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LlopSZWN]|"[^"]*"|'[^']*'/g;
4040
var timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
4141
var timezoneClip = /[^-+\dA-Z]/g; // Regexes and supporting functions are cached through closure
4242

@@ -224,6 +224,14 @@ function _typeof(obj) {
224224
)
225225
);
226226
},
227+
p: function p() {
228+
return (
229+
(_o() > 0 ? "-" : "+") +
230+
pad(Math.floor(Math.abs(_o()) / 60), 2) +
231+
':' +
232+
pad(Math.floor(Math.abs(_o()) % 60), 2)
233+
);
234+
},
227235
S: function S() {
228236
return ["th", "st", "nd", "rd"][
229237
_d() % 10 > 3 ? 0 : (((_d() % 100) - (_d() % 10) != 10) * _d()) % 10

package-lock.json

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dateformat.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"use strict";
1717

1818
const dateFormat = (() => {
19-
const token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZWN]|"[^"]*"|'[^']*'/g;
19+
const token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LlopSZWN]|"[^"]*"|'[^']*'/g;
2020
const timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
2121
const timezoneClip = /[^-+\dA-Z]/g;
2222

@@ -117,6 +117,11 @@
117117
o: () =>
118118
(o() > 0 ? "-" : "+") +
119119
pad(Math.floor(Math.abs(o()) / 60) * 100 + (Math.abs(o()) % 60), 4),
120+
p: () =>
121+
(o() > 0 ? "-" : "+") +
122+
pad(Math.floor(Math.abs(o()) / 60), 2) +
123+
':' +
124+
pad(Math.floor(Math.abs(o()) % 60), 2),
120125
S: () =>
121126
["th", "st", "nd", "rd"][
122127
d() % 10 > 3 ? 0 : (((d() % 100) - (d() % 10) != 10) * d()) % 10

test/test_mask-o.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var assert = require("assert");
2+
var dateFormat = require("../lib/dateformat");
3+
4+
describe("Mask: 'o'", function () {
5+
it("should get timezone for any date as smoething like [+-]XXXX", function (done) {
6+
var date = new Date();
7+
var d = dateFormat(date, "o");
8+
assert.ok(d.match(/^[+-]\d{4}$/), d);
9+
done();
10+
});
11+
});

test/test_mask-p.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var assert = require("assert");
2+
var dateFormat = require("../lib/dateformat");
3+
4+
describe("Mask: 'p'", function () {
5+
it("should get timezone for any date as smoething like [+-]XX:XX", function (done) {
6+
var date = new Date();
7+
var d = dateFormat(date, "p");
8+
assert.ok(d.match(/^[+-]\d{2}:\d{2}$/), d);
9+
done();
10+
});
11+
});

0 commit comments

Comments
 (0)