23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Group integers<br />

var currencyFormatUSD2 =<br />

new Windows.Globalization.NumberFormatting.CurrencyFormatter("USD");<br />

currencyFormatUSD2.isGrouped = 1;<br />

var currencyUSD2 = currencyFormatUSD2.format(fractionalNumber);<br />

The output of this code is as follows:<br />

The other number formatters all work like this, so I’ll leave it to you to check out the details in the<br />

documentation and the sample.<br />

To format dates and time, we can turn to the Windows.Globalization.DateTimeFormatting<br />

namespace where we find the DateTimeFormatter class along with many enumerations for the different<br />

ways to formats seconds, minutes, hours, days, months, and years. To use the API, you instantiate a<br />

formatter object specifying the desired formats and applicable languages. (There are no less than eight<br />

separate constructors here!) You then set options like the clock, geographicRegion, and so forth and call<br />

its format method with the Date value you need to format. You can even apply custom formats if<br />

desired. Many such variations are demonstrated in the Date and time formatting sample; I trust a simple<br />

snippet of its code will suffice here (from Scenario 2, js/stringtemplate.js);<br />

var mydatefmt1 = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(<br />

"month day");<br />

var mytimefmt1 = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(<br />

"hour minute ");<br />

var dateToFormat = new Date();<br />

var mydate1 = mydatefmt1.format(dateToFormat);<br />

var mytime1 = mytimefmt1.format(dateToFormat);<br />

The other bit of code from the SDK that’s relevant here is the Calendar details and math sample. As<br />

mentioned earlier in this chapter when I described expiration times for app trials and in-app purchases,<br />

a world-ready app must not make assumptions about how time periods are computed or compared<br />

because this can vary depending on regional calendars. This is why the extensive<br />

Windows.-Globalization.Calendar class contains ten distinct add* methods that range from<br />

addNanoseconds to addEras, along with its compare and compareDateTime methods (and a bunch to get<br />

all the little bits of calendar-related text). In other words, drill it into your mind now to never, ever use<br />

arithmetic operators on date and time values because they won’t work properly in every locale. Even in<br />

the United States you’ll end up getting wrong answers at times because you won’t be taking things like<br />

daylight savings time into account, where the number of hours in two days of every year will not<br />

actually be 24!<br />

802

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!