Skip to content

Commit a547fbd

Browse files
committed
Allow use of date objects in toLocalTime
Also tidy up the parsing of the ISO time. No idea why it was like that!
1 parent d56af06 commit a547fbd

7 files changed

+28
-28
lines changed

dist/jquery.localtime-0.6.2.min.js

-4
This file was deleted.

dist/jquery.localtime-0.6.2.js dist/jquery.localtime-0.7.0-SNAPSHOT.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! jQuery localtime - v0.6.2 - 2013-02-21
1+
/*! jQuery localtime - v0.7.0-SNAPSHOT - 2013-02-23
22
* https://github.com/GregDThomas/jquery-localtime
33
* Copyright (c) 2013 Greg Thomas; Licensed Apache-2.0 */
44
(function ($) {
@@ -161,14 +161,7 @@
161161
var second = (fields[6] === undefined ? 0 : parseInt(fields[6], 10) );
162162
var millisecond = (fields[7] === undefined ? 0 : parseInt(fields[7], 10) );
163163

164-
var objDate = new Date(2000, 0, 15);
165-
objDate.setUTCFullYear(year);
166-
objDate.setUTCMonth(month);
167-
objDate.setUTCDate(dayOfMonth);
168-
objDate.setUTCHours(hour);
169-
objDate.setUTCMinutes(minute);
170-
objDate.setUTCSeconds(second);
171-
objDate.setUTCMilliseconds(millisecond);
164+
var objDate = new Date(Date.UTC(year, month, dayOfMonth, hour, minute, second, millisecond));
172165

173166
// Now check for invalid dates - e.g. 30 of Feb, 31 of Sep
174167
if( objDate.getUTCFullYear() !== year ||
@@ -188,11 +181,14 @@
188181
}
189182
},
190183

191-
toLocalTime: function (timeString, timeFormat) {
184+
toLocalTime: function (timeField, timeFormat) {
185+
if( timeField.constructor.name !== 'Date' ) {
186+
timeField = $.localtime.parseISOTimeString(timeField);
187+
}
192188
if( timeFormat === '' ) {
193189
timeFormat = undefined;
194190
}
195-
return formatLocalDateTime($.localtime.parseISOTimeString(timeString), timeFormat);
191+
return formatLocalDateTime(timeField, timeFormat);
196192
},
197193

198194
formatObject: function( object, format ) {

dist/jquery.localtime-0.7.0-SNAPSHOT.min.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jquery.localtime.jquery.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"formatting",
1212
"ISO8601"
1313
],
14-
"version": "0.6.2",
14+
"version": "0.7.0-SNAPSHOT",
1515
"author": {
1616
"name": "Greg Thomas"
1717
},
@@ -29,7 +29,7 @@
2929
"bugs": "https://github.com/GregDThomas/jquery-localtime/issues",
3030
"homepage": "https://github.com/GregDThomas/jquery-localtime",
3131
"docs": "https://github.com/GregDThomas/jquery-localtime/wiki/Usage",
32-
"download": "https://github.com/GregDThomas/jquery-localtime/tree/0.6.2/dist",
32+
"download": "https://github.com/GregDThomas/jquery-localtime/tree/0.7.0/dist",
3333
"dependencies": {
3434
"jquery": ">=1.3"
3535
}

samples/sample.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<script>$.localtime.setFormat("yyyy-MM-dd HH:mmzzz");</script>
99
</head>
1010
<body>
11-
The local time of 2010-12-12 19:27:00Z is <span class="localtime">2010-12-12 19:27:00Z</span>
11+
The local time of 2010-12-12 19:27:00Z is <span class="localtime">2010-12-12 19:27:00Z</span><br/>
12+
The current time, in the same format, is <script>document.write($.localtime.toLocalTime( new Date() ));</script><br/>
1213
</body>

src/jquery.localtime.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,7 @@
163163
var second = (fields[6] === undefined ? 0 : parseInt(fields[6], 10) );
164164
var millisecond = (fields[7] === undefined ? 0 : parseInt(fields[7], 10) );
165165

166-
var objDate = new Date(2000, 0, 15);
167-
objDate.setUTCFullYear(year);
168-
objDate.setUTCMonth(month);
169-
objDate.setUTCDate(dayOfMonth);
170-
objDate.setUTCHours(hour);
171-
objDate.setUTCMinutes(minute);
172-
objDate.setUTCSeconds(second);
173-
objDate.setUTCMilliseconds(millisecond);
166+
var objDate = new Date(Date.UTC(year, month, dayOfMonth, hour, minute, second, millisecond));
174167

175168
// Now check for invalid dates - e.g. 30 of Feb, 31 of Sep
176169
if( objDate.getUTCFullYear() !== year ||
@@ -190,11 +183,14 @@
190183
}
191184
},
192185

193-
toLocalTime: function (timeString, timeFormat) {
186+
toLocalTime: function (timeField, timeFormat) {
187+
if( timeField.constructor.name !== 'Date' ) {
188+
timeField = $.localtime.parseISOTimeString(timeField);
189+
}
194190
if( timeFormat === '' ) {
195191
timeFormat = undefined;
196192
}
197-
return formatLocalDateTime($.localtime.parseISOTimeString(timeString), timeFormat);
193+
return formatLocalDateTime(timeField, timeFormat);
198194
},
199195

200196
formatObject: function( object, format ) {

test/jquery.localtime_test.js

+7
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,11 @@
236236
$.localtime.formatPage();
237237
equal("3rd Jan 2011 at 13:39", $('#testNonDefaultClassFormat').text());
238238
});
239+
240+
test("Dates and strings", function() {
241+
$.localtime.setFormat("do MMMMM yyyy 'at' hh:mm:ss.SSS tt zzz"); // Pretty much everything
242+
// We know parsing strings works OK, so ensure that passing in a date that matches the string is identical
243+
equal( $.localtime.toLocalTime(new Date( Date.UTC(2013,1,23,2,16,33,123) )), $.localtime.toLocalTime("2013-02-23 02:16:33.123Z") );
244+
equal( $.localtime.toLocalTime(new Date( Date.UTC(2011,0,3,13,39,30,300) )), $.localtime.toLocalTime("2011-01-03 13:39:30.300Z") );
245+
});
239246
}(jQuery));

0 commit comments

Comments
 (0)