Skip to content

Commit 5f64c52

Browse files
committed
v1.4
1 parent 0c3618e commit 5f64c52

4 files changed

Lines changed: 504 additions & 61 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
SimpleDateFormat for JavaScript
22
===============================
33

4-
Copyright (c) 2013 Noah Cooper
4+
Copyright (c) 2019 Noah Cooper
55
Dual licensed under MIT and GPL licenses (<http://projects.iamnoahcooper.com/license>)
6-
Version: 1.3 (25-JUL-2013)
6+
Version: 1.4 (26-JAN-2019)
77

88
Based on the SimpleDateFormat Java class
9-
(<http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html>)
9+
(<https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html>)
1010

1111
Overview
1212
--------
1313

14+
*With [wide support](https://caniuse.com/internationalization) for the
15+
[Internationalization API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat),
16+
this library is no longer needed in most cases.*
17+
1418
While the JavaScript Date object provides developers with methods for working with the individual pieces
1519
of a given date, what's missing is a native method for formatting dates as a string following a specified
1620
pattern. In Java, SimpleDateFormat provides just that. This project is an attempt to port that

SimpleDateFormat.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SimpleDateFormat.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* SimpleDateFormat for JavaScript - https://github.com/noahcooper/SimpleDateFormatJS
3-
* Copyright (c) 2013 Noah Cooper (noah.cooper@hotmail.com)
3+
* Copyright (c) 2019 Noah Cooper (noah.cooper@hotmail.com)
44
* Dual licensed under MIT and GPL licenses (http://projects.iamnoahcooper.com/license)
5-
* Version: 1.3 (25-JUL-2013)
5+
* Version: 1.4 (26-JAN-2019)
66
*
77
* Based on the SimpleDateFormat Java class
8-
* (http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html)
8+
* (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
99
*/
1010

1111
/*
@@ -293,14 +293,22 @@ function simpleDateFormat(pattern, locale) {
293293
'novembre',
294294
'd&' + '#233;cembre'];
295295
}
296+
var formattedMonthShorthand3 = formattedMonthNames[Number(dateParts.month) - 1].substring(0, 3);
297+
if(locale == 'fr_CA') {
298+
if(formattedMonthShorthand3 == 'f&' + '#') {
299+
formattedMonthShorthand3 = 'f&' + '#233;v';
300+
}
301+
else if(formattedMonthShorthand3 == 'ao&') {
302+
formattedMonthShorthand3 = 'ao&' + '#251;';
303+
}
304+
else if(formattedMonthShorthand3 == 'd&' + '#') {
305+
formattedMonthShorthand3 = 'd&' + '#233;c';
306+
}
307+
}
296308
formattedPart = formattedPart.replace(/MMMM/g, formattedMonthNames[Number(dateParts.month) - 1])
297-
.replace(/MMM/g, formattedMonthNames[Number(dateParts.month) - 1]
298-
.substring(0, 3))
309+
.replace(/MMM/g, formattedMonthShorthand3)
299310
.replace(/MM/g, dateParts.month)
300-
.replace(/M/g, oneDigitNumber(dateParts.month))
301-
.replace(/march/g, 'March')
302-
.replace(/may/g, 'May')
303-
.replace(/Mayo/g, 'mayo');
311+
.replace(/M/g, oneDigitNumber(dateParts.month));
304312

305313
var formattedDayNames = ['Sunday',
306314
'Monday',
@@ -327,14 +335,28 @@ function simpleDateFormat(pattern, locale) {
327335
'vendredi',
328336
'samedi'];
329337
}
338+
var formattedDayShorthand3 = formattedDayNames[dateParts.day].substring(0, 3);
339+
if(locale == 'es_US') {
340+
if(formattedDayShorthand3 == 'mi&') {
341+
formattedDayShorthand3 = 'mi&' + 'eacute;';
342+
}
343+
else if(formattedDayShorthand3 == 's&' + 'a') {
344+
formattedDayShorthand3 = 's&' + 'aacute;b';
345+
}
346+
}
330347
formattedPart = formattedPart.replace(/EEEE/g, formattedDayNames[dateParts.day])
331-
.replace(/EEE/g, formattedDayNames[dateParts.day].substring(0, 3))
332-
.replace(/EE/g, formattedDayNames[dateParts.day].substring(0, 3))
333-
.replace(/E/g, formattedDayNames[dateParts.day].substring(0, 3));
348+
.replace(/EEE/g, formattedDayShorthand3)
349+
.replace(/EE/g, formattedDayShorthand3)
350+
.replace(/E/g, formattedDayShorthand3);
334351

335352
formattedPart = formattedPart.replace(/A/g, dateParts.ampm)
336-
.replace(/april/g, 'April')
337-
.replace(/august/g, 'August');
353+
.replace(/apr/g, 'Apr')
354+
.replace(/aug/g, 'Aug');
355+
356+
if(locale != 'es_US' && locale != 'fr_CA') {
357+
formattedPart = formattedPart.replace(/mar/g, 'Mar')
358+
.replace(/may/g, 'May');
359+
}
338360

339361
return formattedPart;
340362
};

0 commit comments

Comments
 (0)