Skip to content

Commit

Permalink
ISO8601 format support added
Browse files Browse the repository at this point in the history
  • Loading branch information
daviscabral committed Nov 2, 2012
1 parent 8e4b3da commit 21777cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
40 changes: 30 additions & 10 deletions Test.html
Expand Up @@ -8,7 +8,7 @@

<script src="jquery-1.4.min.js" type="text/javascript"></script>
<script src="jquery.dateFormat-1.0.js" type="text/javascript"></script>

<link rel="stylesheet" href="assets/unittest.css" type="text/css" />
</head>
<body>
Expand All @@ -21,7 +21,7 @@ <h1>JavaScript unit test file</h1>

<!-- Log output (one per Runner, via {testLog: "testlog"} option)-->
<div id="testlog"></div>

<!-- Put sample/test html here -->
<div id="sample">
</div>
Expand All @@ -32,11 +32,31 @@ <h1>JavaScript unit test file</h1>
new Test.Unit.Runner({
setup: function() {
},

teardown: function() {

},
testeJavaUtilDateFormat : function() { with(this) {
testISO8601Format : function() { with(this) {
var jsonDate = "2009-12-18T10:54:50Z";
assertEqual('18/12/2009', $.format.date(jsonDate, 'dd/MM/yyyy'));
assertEqual('Dec/2009', $.format.date(jsonDate, 'MMM/yyyy'));
assertEqual('12', $.format.date(jsonDate, 'MM'));
assertEqual('18', $.format.date(jsonDate, 'dd'));
assertEqual('2009', $.format.date(jsonDate, 'yyyy'));
assertEqual('12', $.format.date(jsonDate, 'MM'));
assertEqual('10:54:50', $.format.date(jsonDate, 'hh:mm:ss'));
assertEqual('10', $.format.date(jsonDate, 'hh'));
assertEqual('54', $.format.date(jsonDate, 'mm'));
assertEqual('50', $.format.date(jsonDate, 'ss'));
assertEqual('18/12/2009 10:54:50 AM', $.format.date(jsonDate, 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('z18/b12/c2009 x10:e54:f50', $.format.date(jsonDate, 'zdd/bMM/cyyyy xhh:emm:fss'));

var jsonDate = "2009-12-18T13:54:50Z";
assertEqual('18/12/2009 01:54:50', $.format.date(jsonDate, 'dd/MM/yyyy hh:mm:ss'));
assertEqual('18/12/2009 13:54:50', $.format.date(jsonDate, 'dd/MM/yyyy HH:mm:ss'));
assertEqual('18/12/2009 01:54:50 PM', $.format.date(jsonDate, 'dd/MM/yyyy hh:mm:ss a'));
}},
testJavaUtilDateFormat : function() { with(this) {
/* Java java.util.Date output */
assertEqual('18/12/2009', $.format.date('2009-12-18 10:54:50.546', 'dd/MM/yyyy'));
assertEqual('Dec/2009', $.format.date('2009-12-18 10:54:50.546', 'MMM/yyyy'));
Expand Down Expand Up @@ -73,9 +93,9 @@ <h1>JavaScript unit test file</h1>
assertEqual('13/01/2010 14:43:41', $.format.date('Wed Jan 13 14:43:41 CET 2010', 'dd/MM/yyyy HH:mm:ss'));
assertEqual('13/01/2010 10:43:41 AM', $.format.date('Wed Jan 13 10:43:41 CET 2010', 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('13/01/2010 05:43:41 PM', $.format.date('Wed Jan 13 17:43:41 CET 2010', 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('z13/b01/c2010 x10:e43:f41', $.format.date('Wed Jan 13 10:43:41 CET 2010', 'zdd/bMM/cyyyy xhh:emm:fss'));
assertEqual('z13/b01/c2010 x10:e43:f41', $.format.date('Wed Jan 13 10:43:41 CET 2010', 'zdd/bMM/cyyyy xhh:emm:fss'));
assertEqual('Wednesday', $.format.date('Wed Jan 13 10:43:41 CET 2010', 'ddd'));
}},
}},
testJAXBFormat : function() { with(this) {
/* Java JAXB java.util.Date output */
assertEqual('19/10/2010', $.format.date('2010-10-19T11:40:33.527+02:00', 'dd/MM/yyyy'));
Expand All @@ -92,7 +112,7 @@ <h1>JavaScript unit test file</h1>
assertEqual('19/10/2010 14:43:41', $.format.date('2010-10-19T14:43:41.527+02:000', 'dd/MM/yyyy HH:mm:ss'));
assertEqual('19/10/2010 10:43:41 AM', $.format.date('2010-10-19T10:43:41.527+02:00', 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('19/10/2010 05:43:41 PM', $.format.date('2010-10-19T17:43:41.527+02:00', 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('z19/b10/c2010 x11:e40:f33', $.format.date('2010-10-19T11:40:33.527+02:00', 'zdd/bMM/cyyyy xhh:emm:fss'));
assertEqual('z19/b10/c2010 x11:e40:f33', $.format.date('2010-10-19T11:40:33.527+02:00', 'zdd/bMM/cyyyy xhh:emm:fss'));
assertEqual('Tuesday', $.format.date('2010-10-19T11:40:33.527+02:00', 'ddd'));
}},
testeJavaScriptDateFormat: function() { with(this) {
Expand All @@ -111,7 +131,7 @@ <h1>JavaScript unit test file</h1>
assertEqual('19/10/2010 14:43:41', $.format.date(new Date('10/19/2010 14:43:41').toString(), 'dd/MM/yyyy HH:mm:ss'));
assertEqual('19/10/2010 10:43:41 AM', $.format.date(new Date('10/19/2010 10:43:41').toString(), 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('19/10/2010 05:43:41 PM', $.format.date(new Date('10/19/2010 17:43:41').toString(), 'dd/MM/yyyy hh:mm:ss a'));
assertEqual('z19/b10/c2010 x11:e40:f33', $.format.date(new Date('10/19/2010 11:40:33').toString(), 'zdd/bMM/cyyyy xhh:emm:fss'));
assertEqual('z19/b10/c2010 x11:e40:f33', $.format.date(new Date('10/19/2010 11:40:33').toString(), 'zdd/bMM/cyyyy xhh:emm:fss'));
assertEqual('Tuesday', $.format.date(new Date('10/19/2010 10:43:41').toString(), 'ddd'));
}},
testFormatDateType5: function() { with(this) {
Expand Down Expand Up @@ -226,7 +246,7 @@ <h1>JavaScript unit test file</h1>
assertEqual("1", $.format.date(testDate.getTime(), 'M'));
assertEqual('5', $.format.date('2012-05-07CET00:00:00', 'M'));
}},
});
});
// ]]>
</script>
</body>
Expand Down
19 changes: 10 additions & 9 deletions jquery.dateFormat-1.0.js
@@ -1,8 +1,8 @@
(function (jQuery) {

var daysInWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var shortMonthsInYear = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var longMonthsInYear = ["January", "February", "March", "April", "May", "June",
var longMonthsInYear = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var shortMonthsToNumber = [];
shortMonthsToNumber["Jan"] = "01";
Expand All @@ -17,7 +17,7 @@
shortMonthsToNumber["Oct"] = "10";
shortMonthsToNumber["Nov"] = "11";
shortMonthsToNumber["Dec"] = "12";

jQuery.format = (function () {
function strDay(value) {
return daysInWeek[parseInt(value, 10)] || value;
Expand All @@ -30,7 +30,7 @@

function strLongMonth(value) {
var monthArrayIndex = parseInt(value, 10) - 1;
return longMonthsInYear[monthArrayIndex] || value;
return longMonthsInYear[monthArrayIndex] || value;
}

var parseMonth = function (value) {
Expand Down Expand Up @@ -73,9 +73,9 @@

return {
date: function (value, format) {
/*
/*
value = new java.util.Date()
2009-12-18 10:54:50.546
2009-12-18 10:54:50.546
*/
try {
var date = null;
Expand All @@ -92,7 +92,8 @@
dayOfMonth = value.getDate();
dayOfWeek = value.getDay();
time = parseTime(value.toTimeString());
} else if (value.search(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d{0,3}[-+]?\d{2}:?\d{2}/) != -1) { /* 2009-04-19T16:11:05+02:00 */
} else if (value.search(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d{0,3}[Z\-+]?(\d{2}:?\d{2})?/) != -1) {
/* 2009-04-19T16:11:05+02:00 || 2009-04-19T16:11:05Z */
var values = value.split(/[T\+-]/);
year = values[0];
month = values[1];
Expand Down Expand Up @@ -154,7 +155,7 @@
var retValue = "";
var unparsedRest = "";
/*
Issue 1 - variable scope issue in format.date
Issue 1 - variable scope issue in format.date
Thanks jakemonO
*/
for (var i = 0; i < format.length; i++) {
Expand Down Expand Up @@ -238,7 +239,7 @@
if (format.charAt(i + 1) == "h") {
break;
}
var hour = (time.hour == 0 ? 12 : time.hour < 13 ? time.hour : time.hour - 12);
var hour = (time.hour == 0 ? 12 : time.hour < 13 ? time.hour : time.hour - 12);
retValue += parseInt(hour, 10);
// Fixing issue https://github.com/phstc/jquery-dateFormat/issues/21
// retValue = parseInt(retValue, 10);
Expand Down

0 comments on commit 21777cf

Please sign in to comment.