Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #73 from prozacgod/master

Added 12 hour string formater
  • Loading branch information...
commit 17a9721fb300033524c9fcbe6a7b19932f862372 2 parents c4dde30 + f88df0c
@longlho longlho authored
Showing with 35 additions and 0 deletions.
  1. +33 −0 spec/date.spec.js
  2. +2 −0  src/date.js
View
33 spec/date.spec.js
@@ -463,4 +463,37 @@ describe('timezoneJS.Date', function () {
date.setYear(2003);
expect(date.getFullYear()).toEqual(2003);
});
+
+ it('Represents hours in 24 hour format', function () {
+ for (var i = 0; i < 24; i ++) {
+ var time_value = new timezoneJS.Date(2013, 1, 1, i, 0, 0, 0, "America/Chicago");
+ expect(time_value.toString("H")).toEqual(i.toString());
+ }
+ });
+
+ xit('Represents hours in 12 hour format by k side effect', function () {
+ for (var i = 0; i < 24; i ++) {
+ var ampm = i >= 12 ? "PM" : "AM";
+ var hour = (i % 12);
+ hour = (hour === 0) ? '12' : hour.toString();
+ zhour = hour > 9 ? hour.toString() : '0' + hour;
+
+ var time_value = new timezoneJS.Date(2013, 1, 1, i, 0, 0, 0, "America/Chicago");
+ expect(time_value.toString("H k")).toEqual(hour + " " + ampm);
+ expect(time_value.toString("HH k")).toEqual(zhour + " " + ampm);
+ }
+ });
+
+ it('Represents hours in single digit 12 hour format', function () {
+ for (var i = 0; i < 24; i ++) {
+ var ampm = i >= 12 ? "PM" : "AM";
+ var hour = (i % 12);
+ hour = (hour === 0) ? '12' : hour.toString();
+ zhour = hour > 9 ? hour.toString() : '0' + hour;
+
+ var time_value = new timezoneJS.Date(2013, 1, 1, i, 0, 0, 0, "America/Chicago");
+ expect(time_value.toString("h k")).toEqual(hour + " " + ampm);
+ expect(time_value.toString("hh k")).toEqual(zhour + " " + ampm);
+ }
+ });
});
View
2  src/date.js
@@ -473,6 +473,8 @@
})
// `H`: hour
.replace(/H+/g, function (token) { return _fixWidth(hours, token.length); })
+ // 'h': 12 hour format
+ .replace(/h+/g, function (token) { return _fixWidth( ((hours%12) === 0) ? 12 : (hours % 12), token.length); })
// `E`: day
.replace(/E+/g, function (token) { return DAYS[_this.getDay()].substring(0, token.length); })
// `Z`: timezone abbreviation
Please sign in to comment.
Something went wrong with that request. Please try again.