Skip to content

Commit ea4d1a1

Browse files
committed
include Day of the Week formatting. (GregDThomas#13)
1 parent 5609663 commit ea4d1a1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/jquery.localtime.js

+6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414
'July', 'August', 'September',
1515
'October', 'November', 'December'];
1616

17+
var longDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
18+
1719
var ordinals = ['th', 'st', 'nd', 'rd'];
1820

1921
var amPmHour = function (hour) {
2022
return (hour >= 13) ? (hour - 12) : ((hour === "0") ? 12 : hour);
2123
};
2224

2325
var formatLocalDateTime = function (objDate, timeFormat) {
26+
// Note that some fields are stored strings, as we slice and/or add a "0" prefix in some cases.
2427
var year = objDate.getFullYear().toString();
2528
var month = (objDate.getMonth() + 1).toString();
2629
var date = objDate.getDate().toString();
30+
var dow = objDate.getDay();
2731
var hour = objDate.getHours().toString();
2832
var minute = objDate.getMinutes().toString();
2933
var second = objDate.getSeconds().toString();
@@ -77,6 +81,8 @@
7781
switch (pattern) {
7882
case "d": formattedDate += date; break;
7983
case "dd": formattedDate += ("0" + date).slice(-2); break;
84+
case "ddd": formattedDate += longDays[dow].substr(0, 3); break;
85+
case "ddddd": formattedDate += longDays[dow]; break;
8086
case "M": formattedDate += month; break;
8187
case "MM": formattedDate += ("0" + month).slice(-2); break;
8288
case "MMM": formattedDate += longMonths[month - 1].substr(0, 3); break;

test/jquery.localtime_test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@
8383
$.localtime.setFormat("dd");
8484
equal("03", $.localtime.toLocalTime("2011-01-03 13:39:30.300Z") );
8585
});
86+
test("Long day formats", function() {
87+
$.localtime.setFormat("ddd");
88+
equal("Sun", $.localtime.toLocalTime("2011-01-02 13:39:30.300Z") );
89+
$.localtime.setFormat("ddddd");
90+
equal("Sunday", $.localtime.toLocalTime("2011-01-02 13:39:30.300Z") );
91+
$.localtime.setFormat("ddd");
92+
equal("Sat", $.localtime.toLocalTime("2014-01-11 13:39:30.300Z") );
93+
$.localtime.setFormat("ddddd");
94+
equal("Saturday", $.localtime.toLocalTime("2014-01-11 13:39:30.300Z") );
95+
});
8696
test("Month formats", function() {
8797
$.localtime.setFormat("M");
8898
equal("1", $.localtime.toLocalTime("2011-01-03 13:39:30.300Z") );
@@ -271,7 +281,7 @@
271281
equal("3rd Jan 2011 at 13:39", $('#innerSpanClass').text()); // Formatted
272282
});
273283

274-
test("Default scope (whoele page)", function() {
284+
test("Default scope (whole page)", function() {
275285
// NB. Following is require to prevent the formatting be applied to already
276286
// formatted date/times
277287
$('[data-localtime-format]').removeAttr('data-localtime-format');

0 commit comments

Comments
 (0)