@alexbald/nativescript-intl
by alexbald | v4.0.2
提供与Intl.js类似的API,用于在NativeScript中使用本地日期、时间和数字格式
npm i --save @alexbald/nativescript-intl

欢迎使用NativeScript框架的nativescript-intl插件

先决条件

安装 nativescript-intl 插件。

导航到项目文件夹,并运行 NativeScript-CLI 命令 tns plugin add nativescript-intl

此插件提供了类似于 Intl.js API,用于移动设备(Android和iOS)上的日期、时间和数字格式化。

使用方法

var intl = require("nativescript-intl");
var dateFormat = new intl.DateTimeFormat('en-US', {'year': 'numeric', 'month': 'short', 'day': 'numeric'}).format(new Date(2016, 2, 23));
var numberFormat = new intl.NumberFormat('en-US', {'style': 'currency', 'currency': 'USD', 'currencyDisplay': 'symbol'}).format(123456.789);

console.log("dateFormat: " + dateFormat);
console.log("numberFormat: " + numberFormat);
// prints Mar 23, 2016
// $123456.79

由于Android、iOS和 Intl.js 上的本地化格式化不完全相同,因此还可以选择使用此API的另一种选项。

自定义模式

var intl = require("nativescript-intl");
var dateFormat = new intl.DateTimeFormat(null, null, 'y MMMM d, EEEE HH:mm:ss').format(new Date(2016, 2, 23, 12, 35, 40));
var numberFormat = new intl.NumberFormat('en-US', {'style': 'currency', 'currency': 'USD', 'currencyDisplay': 'symbol'}, '#,##0.00 ¤').format(123456.789);

console.log("dateFormat: " + dateFormat);
console.log("numberFormat: " + numberFormat);
// prints 2016 March 23, Wednesday 2016 12:35:40
// 123,456.79 $

省略区域设置将使用设备上的当前区域设置。

请注意,ICU版本在不同设备操作系统版本之间有所不同,因此此插件可能在不同的API级别(Android)和操作系统版本(iOS)上产生不同的结果。