date-format 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/.npmignore ADDED
@@ -0,0 +1,15 @@
1
+ lib-cov
2
+ *.seed
3
+ *.log
4
+ *.csv
5
+ *.dat
6
+ *.out
7
+ *.pid
8
+ *.gz
9
+
10
+ pids
11
+ logs
12
+ results
13
+
14
+ npm-debug.log
15
+ node_modules
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Gareth Jones
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ date-format
2
+ ===========
3
+
4
+ node.js formatting of Date objects as strings. Probably exactly the same as some other library out there.
5
+
6
+ npm install date-format
7
+
8
+ usage
9
+ =====
10
+
11
+ var format = require('date-format');
12
+ format.asString(new Date()); //defaults to ISO8601 format
13
+ format.asString('hh:mm:ss.SSS', new Date()); //just the time
14
+
15
+ Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
16
+ * dd - date.getDate()
17
+ * MM - date.getMonth() + 1
18
+ * yy - date.getFullYear().toString().substring(2, 4)
19
+ * yyyy - date.getFullYear()
20
+ * hh - date.getHours()
21
+ * mm - date.getMinutes()
22
+ * ss - date.getSeconds()
23
+ * SSS - date.getMilliseconds()
24
+ * O - timezone offset in +hm format
25
+
26
+ That's it.
package/lib/index.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ exports.ISO8601_FORMAT = "yyyy-MM-dd hh:mm:ss.SSS";
3
+ exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ssO";
4
+ exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
5
+ exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
6
+
7
+ function padWithZeros(vNumber, width) {
8
+ var numAsString = vNumber + "";
9
+ while (numAsString.length < width) {
10
+ numAsString = "0" + numAsString;
11
+ }
12
+ return numAsString;
13
+ }
14
+
15
+ function addZero(vNumber) {
16
+ return padWithZeros(vNumber, 2);
17
+ }
18
+
19
+ /**
20
+ * Formats the TimeOffest
21
+ * Thanks to http://www.svendtofte.com/code/date_format/
22
+ * @private
23
+ */
24
+ function offset(date) {
25
+ // Difference to Greenwich time (GMT) in hours
26
+ var os = Math.abs(date.getTimezoneOffset());
27
+ var h = String(Math.floor(os/60));
28
+ var m = String(os%60);
29
+ if (h.length == 1) {
30
+ h = "0" + h;
31
+ }
32
+ if (m.length == 1) {
33
+ m = "0" + m;
34
+ }
35
+ return date.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
36
+ }
37
+
38
+ exports.asString = function(/*format,*/ date) {
39
+ var format = exports.ISO8601_FORMAT;
40
+ if (typeof(date) === "string") {
41
+ format = arguments[0];
42
+ date = arguments[1];
43
+ }
44
+
45
+ var vDay = addZero(date.getDate());
46
+ var vMonth = addZero(date.getMonth()+1);
47
+ var vYearLong = addZero(date.getFullYear());
48
+ var vYearShort = addZero(date.getFullYear().toString().substring(2,4));
49
+ var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort);
50
+ var vHour = addZero(date.getHours());
51
+ var vMinute = addZero(date.getMinutes());
52
+ var vSecond = addZero(date.getSeconds());
53
+ var vMillisecond = padWithZeros(date.getMilliseconds(), 3);
54
+ var vTimeZone = offset(date);
55
+ var formatted = format
56
+ .replace(/dd/g, vDay)
57
+ .replace(/MM/g, vMonth)
58
+ .replace(/y{1,4}/g, vYear)
59
+ .replace(/hh/g, vHour)
60
+ .replace(/mm/g, vMinute)
61
+ .replace(/ss/g, vSecond)
62
+ .replace(/SSS/g, vMillisecond)
63
+ .replace(/O/g, vTimeZone);
64
+ return formatted;
65
+
66
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "date-format",
3
+ "version": "0.0.0",
4
+ "description": "formatting Date objects as strings since 2013",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "test": "mocha"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/nomiddlename/date-format.git"
12
+ },
13
+ "keywords": [
14
+ "date",
15
+ "format",
16
+ "string"
17
+ ],
18
+ "author": "Gareth Jones <gareth.jones@sensis.com.au>",
19
+ "license": "MIT",
20
+ "readmeFilename": "README.md",
21
+ "gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
22
+ "devDependencies": {
23
+ "should": "~1.2.2",
24
+ "mocha": "~1.12.0"
25
+ }
26
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var should = require('should')
3
+ , dateFormat = require('../lib');
4
+
5
+ describe('date_format', function() {
6
+ var date = new Date(2010, 0, 11, 14, 31, 30, 5);
7
+
8
+ it('should format a date as string using a pattern', function() {
9
+ dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql("11 01 2010 14:31:30.005");
10
+ });
11
+
12
+ it('should default to the ISO8601 format', function() {
13
+ dateFormat.asString(date).should.eql('2010-01-11 14:31:30.005');
14
+ });
15
+
16
+ it('should provide a ISO8601 with timezone offset format', function() {
17
+ date.getTimezoneOffset = function() { return -660; };
18
+ dateFormat.asString(
19
+ dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
20
+ date
21
+ ).should.eql(
22
+ "2010-01-11T14:31:30+1100"
23
+ );
24
+
25
+ date.getTimezoneOffset = function() { return 120; };
26
+ dateFormat.asString(
27
+ dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
28
+ date
29
+ ).should.eql(
30
+ "2010-01-11T14:31:30-0200"
31
+ );
32
+ });
33
+
34
+ it('should provide a just-the-time format', function() {
35
+ dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
36
+ });
37
+
38
+ it('should provide a custom format', function() {
39
+ date.getTimezoneOffset = function() { return 120; };
40
+ dateFormat.asString("O.SSS.ss.mm.hh.dd.MM.yy", date).should.eql('-0200.005.30.31.14.11.01.10');
41
+ });
42
+ });