1- /*!code-snippet v1.1.3 | NHN Entertainment*/
1+ /*!code-snippet v1.2.0 | NHN Entertainment*/
22/**********
33 * array.js
44 **********/
@@ -1648,86 +1648,86 @@ tui.util.Enum = Enum;
16481648 * @fileoverview This module has a function for date format.
16491649 * @author NHN Ent.
16501650 * FE Development Team <e0242@nhnent.com>
1651- * @dependency type.js
1651+ * @dependency type.js, object.js
16521652 */
16531653
16541654( function ( tui ) {
16551655 'use strict' ;
16561656
1657- var tokens = / [ \\ ] * Y Y Y Y | [ \\ ] * Y Y | [ \\ ] * M M M M | [ \\ ] * M M M | [ \\ ] * M M | [ \\ ] * M | [ \\ ] * D D | [ \\ ] * D | [ \\ ] * H H | [ \\ ] * H | [ \\ ] * A / gi,
1658- MONTH_STR = [ "Invalid month" , "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" ] ,
1659- MONTH_DAYS = [ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ,
1660- replaceMap = {
1661- M : function ( date ) {
1662- return Number ( date . month ) ;
1663- } ,
1664- MM : function ( date ) {
1665- var month = date . month ;
1666- return ( Number ( month ) < 10 ) ? '0' + month : month ;
1667- } ,
1668- MMM : function ( date ) {
1669- return MONTH_STR [ Number ( date . month ) ] . substr ( 0 , 3 ) ;
1670- } ,
1671- MMMM : function ( date ) {
1672- return MONTH_STR [ Number ( date . month ) ] ;
1673- } ,
1674- D : function ( date ) {
1675- return Number ( date . date ) ;
1676- } ,
1677- d : function ( date ) {
1678- return replaceMap . D ( date ) ;
1679- } ,
1680- DD : function ( date ) {
1681- var dayInMonth = date . date ;
1682- return ( Number ( dayInMonth ) < 10 ) ? '0' + dayInMonth : dayInMonth ;
1683- } ,
1684- dd : function ( date ) {
1685- return replaceMap . DD ( date ) ;
1686- } ,
1687- YY : function ( date ) {
1688- return Number ( date . year ) % 100 ;
1689- } ,
1690- yy : function ( date ) {
1691- return replaceMap . YY ( date ) ;
1692- } ,
1693- YYYY : function ( date ) {
1694- var prefix = '20' ,
1695- year = date . year ;
1696- if ( year > 69 && year < 100 ) {
1697- prefix = '19' ;
1698- }
1699- return ( Number ( year ) < 100 ) ? prefix + String ( year ) : year ;
1700- } ,
1701- yyyy : function ( date ) {
1702- return replaceMap . YYYY ( date ) ;
1703- } ,
1704- A : function ( date ) {
1705- return date . meridian ;
1706- } ,
1707- a : function ( date ) {
1708- return date . meridian . toLowerCase ( ) ;
1709- } ,
1710- hh : function ( date ) {
1711- var hour = date . hour ;
1712- return ( Number ( hour ) < 10 ) ? '0' + hour : hour ;
1713- } ,
1714- HH : function ( date ) {
1715- return replaceMap . hh ( date ) ;
1716- } ,
1717- h : function ( date ) {
1718- return String ( Number ( date . hour ) ) ;
1719- } ,
1720- H : function ( date ) {
1721- return replaceMap . h ( date ) ;
1722- } ,
1723- m : function ( date ) {
1724- return String ( Number ( date . minute ) ) ;
1725- } ,
1726- mm : function ( date ) {
1727- var minute = date . minute ;
1728- return ( Number ( minute ) < 10 ) ? '0' + minute : minute ;
1657+ var tokens = / [ \\ ] * Y Y Y Y | [ \\ ] * Y Y | [ \\ ] * M M M M | [ \\ ] * M M M | [ \\ ] * M M | [ \\ ] * M | [ \\ ] * D D | [ \\ ] * D | [ \\ ] * H H | [ \\ ] * H | [ \\ ] * A / gi;
1658+ var MONTH_STR = [ "Invalid month" , "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" ] ;
1659+ var MONTH_DAYS = [ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ;
1660+ var replaceMap = {
1661+ M : function ( date ) {
1662+ return Number ( date . month ) ;
1663+ } ,
1664+ MM : function ( date ) {
1665+ var month = date . month ;
1666+ return ( Number ( month ) < 10 ) ? '0' + month : month ;
1667+ } ,
1668+ MMM : function ( date ) {
1669+ return MONTH_STR [ Number ( date . month ) ] . substr ( 0 , 3 ) ;
1670+ } ,
1671+ MMMM : function ( date ) {
1672+ return MONTH_STR [ Number ( date . month ) ] ;
1673+ } ,
1674+ D : function ( date ) {
1675+ return Number ( date . date ) ;
1676+ } ,
1677+ d : function ( date ) {
1678+ return replaceMap . D ( date ) ;
1679+ } ,
1680+ DD : function ( date ) {
1681+ var dayInMonth = date . date ;
1682+ return ( Number ( dayInMonth ) < 10 ) ? '0' + dayInMonth : dayInMonth ;
1683+ } ,
1684+ dd : function ( date ) {
1685+ return replaceMap . DD ( date ) ;
1686+ } ,
1687+ YY : function ( date ) {
1688+ return Number ( date . year ) % 100 ;
1689+ } ,
1690+ yy : function ( date ) {
1691+ return replaceMap . YY ( date ) ;
1692+ } ,
1693+ YYYY : function ( date ) {
1694+ var prefix = '20' ,
1695+ year = date . year ;
1696+ if ( year > 69 && year < 100 ) {
1697+ prefix = '19' ;
17291698 }
1730- } ;
1699+ return ( Number ( year ) < 100 ) ? prefix + String ( year ) : year ;
1700+ } ,
1701+ yyyy : function ( date ) {
1702+ return replaceMap . YYYY ( date ) ;
1703+ } ,
1704+ A : function ( date ) {
1705+ return date . meridian ;
1706+ } ,
1707+ a : function ( date ) {
1708+ return date . meridian ;
1709+ } ,
1710+ hh : function ( date ) {
1711+ var hour = date . hour ;
1712+ return ( Number ( hour ) < 10 ) ? '0' + hour : hour ;
1713+ } ,
1714+ HH : function ( date ) {
1715+ return replaceMap . hh ( date ) ;
1716+ } ,
1717+ h : function ( date ) {
1718+ return String ( Number ( date . hour ) ) ;
1719+ } ,
1720+ H : function ( date ) {
1721+ return replaceMap . h ( date ) ;
1722+ } ,
1723+ m : function ( date ) {
1724+ return String ( Number ( date . minute ) ) ;
1725+ } ,
1726+ mm : function ( date ) {
1727+ var minute = date . minute ;
1728+ return ( Number ( minute ) < 10 ) ? '0' + minute : minute ;
1729+ }
1730+ } ;
17311731
17321732 /* istanbul ignore if */
17331733 if ( ! tui ) {
@@ -1777,18 +1777,19 @@ tui.util.Enum = Enum;
17771777 * Return a string that transformed from the given form and date.
17781778 * @param {string } form - Date form
17791779 * @param {Date|Object } date - Date object
1780+ * @param {{meridianSet: {AM: string, PM: string}} } option - Option
17801781 * @returns {boolean|string } A transformed string or false.
17811782 * @memberOf tui.util
17821783 * @example
1783- * // key | Shorthand
1784- * // ------------|-----------------------
1785- * // years | YY / YYYY / yy / yyyy
1786- * // months(n) | M / MM
1787- * // months(str) | MMM / MMMM
1788- * // days | D / DD / d / dd
1789- * // hours | H / HH / h / hh
1790- * // minutes | m / mm
1791- * // AM/PM | A / a
1784+ * // key | Shorthand
1785+ * // --------------- |-----------------------
1786+ * // years | YY / YYYY / yy / yyyy
1787+ * // months(n) | M / MM
1788+ * // months(str) | MMM / MMMM
1789+ * // days | D / DD / d / dd
1790+ * // hours | H / HH / h / hh
1791+ * // minutes | m / mm
1792+ * // meridian(AM,PM) | A / a
17921793 *
17931794 * var dateStr1 = formatDate('yyyy-MM-dd', {
17941795 * year: 2014,
@@ -1810,11 +1811,20 @@ tui.util.Enum = Enum;
18101811 * dateStr3 = formatDate('yyyy년 M월 dd일', dt);
18111812 *
18121813 * alert(dateStr3); // '2010년 3월 13일'
1814+ *
1815+ * var option4 = {
1816+ * meridianSet: {
1817+ * AM: '오전',
1818+ * PM: '오후'
1819+ * }
1820+ * };
1821+ * var date4 = {year: 1999, month: 9, date: 9, hour: 13, minute: 2};
1822+ * var dateStr4 = formatDate('yyyy-MM-dd A hh:mm', date4, option4));
1823+ *
1824+ * alert(dateStr4); // '1999-09-09 오후 01:02'
18131825 */
1814- function formatDate ( form , date ) {
1815- var meridian ,
1816- nDate ,
1817- resultStr ;
1826+ function formatDate ( form , date , option ) {
1827+ var meridian , nDate , resultStr ;
18181828
18191829 if ( tui . util . isDate ( date ) ) {
18201830 nDate = {
@@ -1840,13 +1850,15 @@ tui.util.Enum = Enum;
18401850
18411851 nDate . meridian = '' ;
18421852 if ( / [ ^ \\ ] [ a A ] \b / g. test ( form ) ) {
1843- meridian = ( nDate . hour > 12 ) ? 'PM' : 'AM' ;
1853+ meridian = ( nDate . hour > 12 ) ?
1854+ tui . util . pick ( option , 'meridianSet' , 'PM' ) || 'PM'
1855+ : tui . util . pick ( option , 'meridianSet' , 'AM' ) || 'AM' ;
18441856 nDate . hour %= 12 ;
18451857 nDate . meridian = meridian ;
18461858 }
18471859
18481860 resultStr = form . replace ( tokens , function ( key ) {
1849- if ( key . indexOf ( '\\' ) > - 1 ) {
1861+ if ( key . indexOf ( '\\' ) > - 1 ) { // escape character
18501862 return key . replace ( / \\ / g, '' ) ;
18511863 } else {
18521864 return replaceMap [ key ] ( nDate ) || '' ;
0 commit comments