YUI Library Home

YUI Library Examples: Calendar Control: Localization - Taiwan (Year Offset)

Calendar Control: Localization - Taiwan (Year Offset)

Along with the locale customization parameters covered in the Japanese and German calendar examples, Calendar also supports the ability to set a year offset (from the Gregorian year), which can be used to support locales such as Taiwan (zh-TW), for which the year is 1911 years behind the Gregorian year. This example demonstrates how the year offset field can be used to create a Calendar for the zh-TW locale.

星期日 星期一 星期二 星期三 星期四 星期五 星期六
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Setting up the Calendar

As with the other localization examples, we set up the various label, position and delimiter fields for the Taiwan (zh-TW) localized calendar:

1// We set the year_offset configuration field to -1911 
2YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1Container", {  
3            MULTI_SELECT:true,  
4            YEAR_OFFSET:-1911  
5} ); 
6 
7// Year/Month/Date formats for zh-TW: yyyy/mm/dd, mm/dd, yyyy/mm 
8YAHOO.example.calendar.cal1.cfg.setProperty("MDY_YEAR_POSITION", 1); 
9YAHOO.example.calendar.cal1.cfg.setProperty("MDY_MONTH_POSITION", 2); 
10YAHOO.example.calendar.cal1.cfg.setProperty("MDY_DAY_POSITION", 3); 
11 
12YAHOO.example.calendar.cal1.cfg.setProperty("MY_YEAR_POSITION", 1); 
13YAHOO.example.calendar.cal1.cfg.setProperty("MY_MONTH_POSITION", 2); 
14 
15// Date labels for zh-TW 
16var months = ["\u4e00\u6708","\u4e8c\u6708""\u4e09\u6708""\u56db\u6708""\u4e94\u6708""\u516d\u6708""\u4e03\u6708""\u516b\u6708""\u4e5d\u6708""\u5341\u6708""\u5341\u4e00\u6708""\u5341\u4e8c\u6708"]; 
17var weekdays = ["\u661f\u671f\u65e5""\u661f\u671f\u4e00""\u661f\u671f\u4e8c""\u661f\u671f\u4e09""\u661f\u671f\u56db""\u661f\u671f\u4e94""\u661f\u671f\u516d"]; 
18 
19YAHOO.example.calendar.cal1.cfg.setProperty("MONTHS_SHORT", months); 
20YAHOO.example.calendar.cal1.cfg.setProperty("MONTHS_LONG", months); 
21 
22YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_1CHAR", weekdays); 
23YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_SHORT", weekdays); 
24YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_MEDIUM", weekdays); 
25YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_LONG", weekdays); 
26 
27// Month/Year label format for zh-TW 
28YAHOO.example.calendar.cal1.cfg.setProperty("MY_LABEL_YEAR_POSITION",  1); 
29YAHOO.example.calendar.cal1.cfg.setProperty("MY_LABEL_MONTH_POSITION",  2); 
30YAHOO.example.calendar.cal1.cfg.setProperty("MY_LABEL_YEAR_SUFFIX",  "\u5E74"); 
view plain | print | ?

However for the Taiwan localized calendar, we also specify a year offset for the Calendar as shown on line 4 above. The year offset is applied whenever the Calendar renders a year as a string, or accepts a year as string input.

For example, when setting selector dates, or the page date using strings, we use the offset date (the Gregorian year-1911). The months and dates are the same as the Gregorian calendar:

1// When working with strings, we use the offset year... 
2YAHOO.example.calendar.cal1.select("98/8/1-98/8/8"); 
3YAHOO.example.calendar.cal1.cfg.setProperty("PAGEDATE""98/8"); 
4 
5// When working with Date objects, we need to have it reflect the Gregorian date... 
6YAHOO.example.calendar.cal1.cfg.setProperty("MAXDATE"new Date(2009, 7, 20)); 
7YAHOO.example.calendar.cal1.render(); 
view plain | print | ?

It's important to note that whenever you're working with JavaScript Date objects, the year will/should always be set to the Gregorian year (and not offset by the year_offset amount), since the JavaScript Date object's month and date rollovers and leap year calculations will only work correctly when operating with the year set to the equivalent Gregorian year.

For this reason, any JavaScript Date objects returned by Calendar's methods, will always reflect the Gregorian year, even when a year offset is set, and any Date objects passed into Calendar methods, should reflect the Gregorian year.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings