Data Formatting
Date and Times
Dates and times are formatted differently in different locales. These are all the same date:
Locale | Format |
---|---|
en-US | March 2, 2021 at 3:38 PM |
en-GB | 2 March 2021 at 15:38 |
ko-KR | 2021년 3월 2일 오후 3:38 |
de-DE | 2. März 2021 um 15:38 |
zh-Hans-CN | 2021年3月2日 下午3:38 |
Date Formatting
Date and time formatting is done through the DateFmt formatter class Constructor takes options controlling how the formatter behaves Call the format() method of the formatter as many times as you want to format dates with those options
var DateFactory = require("<path-to-ilib>/lib/DateFactory");var date = new Date(2021, 7, 20);var fmt = new DateFmt({locale:"ko-KR", length:"full"});fmt.format(date);// 2021년 8월 20일var fmt = new DateFmt({locale:"en-US", length:"full"});// August 19, 2021var fmt = new DateFmt({locale:"en-GB", length:"full"});// 19 August 2021
Date/Time Formatting Options
There are options to control: If you see more in detail, Please visit the link.
- locale: Which locale to use
- type: Format the date only, time only, or both the date and time together
- clock: Use the 12 or 24 hour clock, or use the default for the locale
- length: Use short, medium, long, or full length text for components that use words.
- and more
var DateFactory=require("<path-to-ilib>/lib/DateFactory");var DateFmt = require("<path-to-ilib>/lib/DateFmt.js")var date= DateFactory({year:2021, month: 8, day:22, hour:15, minute: 22});var fmt = new DateFmt({locale: "en-US",type: "datetime",length: "full",timezone: "local"});var result = fmt.format(date);// August 22, 2021 at 3:22 PMvar fmt = new DateFmt({locale: "ko-KR",date: "dmwy",length: "short",timezone: "local"});var result = fmt.format(date);// 21. 8. 22. 일
Date-related Formatting
Date RangeRange
Date/time ranges are periods of time with a specific start and end points It Can be formatted using the DateRngFmt class Example output: "Mar 11-14, 2013"
var DateFactory = require("<path-to-ilib>/lib/DateFactory");var DateRngFmt = require("<path-to-ilib>/lib/DateRngFmt.js")var start = DateFactory({year:2021, month:3, day:4, hour:14, minute:19});var end = DateFactory({year:2021, month:4, day:14, hour:14, minute:19});var fmt = new DateRngFmt({locale:"de-DE", length:"full"});var d = fmt.format(start, end);// 4. März – 14. April 2021
Duration
Durations are a measure of how long something took to happen It Can be formatted using the DurFmt class Example output: "36 hours, 24 minutes, and 37 seconds"
var DurFmt = require("<path-to-ilib>/lib/DurationFmt.js")var fmt = new DurFmt({locale:"en-US", length:"full"});var result = fmt.format({year:1, month:1, week:1, day:1,hour:1, minute:1, second:1}).toString();// 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute, 1 secondvar result = fmt.format({year:2, month:2, week:2, day:2,hour:2, minute:2, second:2}).toString();// 2 years, 2 months, 2 weeks, 2 days, 2 hours, 2 minutes, 2 seconds
Calendar
Calendar Conversion
iLib supports dates in multiple calendaring systems:
- Coptic
- Ethiopic
- Gregorian
- Han
- Hebrew
- Islamic
- Julian
- Persian
- PersianAlgo
- ThaiSolar
Default is the familiar Gregorian calendar
Create dates using the factory method or using the calendar dates directly:
var HebrewDate = require("<path-to-ilib>/lib/HebrewDate.js")var now = new HebrewDate();// This is equivalent to the factory method:var DateFactory = require("<path-to-ilib>/lib/DateFactory.js")var now = DateFactory({type: "hebrew"});
Calendar Information
Information about a calendar can be retrieved using the Cal.* classes
var PersianCal = require("<path-to-ilib>/lib/PersianCal.js");var cal = new PersianCal();cal.getNumMonths(1392)// 12
Use a factory method to create the other calendar types
var CalendarFactory = require("<path-to-ilib>/lib/CalendarFactory.js");var cal = CalendarFactory({// looks up calendar for this localelocale: "nl-NL"});var days = cal.getMonLength(2, year);// days is 28 in regular years, 29 in leap years
Formatting in Other Calendars
var DateFactory = require("<path-to-ilib>/lib/DateFactory.js");var DateFmt = require("<path-to-ilib>/lib/DateFmt.js");var date= DateFactory({year:2021,month: 8,day:20,hour:15,minute: 22,type:"gregorian"});var fmt=new DateFmt({locale:"am-ET", length:"full"}); // The default calendar is Ethiopicvar reesult = fmt.format(date);// 14 ነሐሴ 2013
Numbers
Number, currency, and percentage formats are locale-sensitive
Locale | Float | Currency | Percentage |
---|---|---|---|
en-US | 123,456,789 | $123.00 | 57.2% |
de-DE | 123.456.789 | 123,00 € | 57,2 % |
fr-FR | 123 456 789 | 123,00 € | 57,2 % |
tr-TR | 123.456.789 | ₺123,00 | %57,2 |
Numeric Formatting
Formatting numbers is achieved with the NumFmt class
var NumFmt = require("<path-to-ilib>/lib/NumFmt.js");var fmt = new NumFmt({locale: "de-DE"});var str = fmt.format(1234567.89);// 1.234.567,89
Currency Formatting
Currency is formatted with the same NumFmt class:
var NumFmt = require("<path-to-ilib>/lib/NumFmt.js");var fmt = new NumFmt({type: "currency",currency: "EUR",locale: "de-DE"});var amount = fmt.format(1234.56289);// 1.234,56 €
Percentage Formatting
Percentages are formatted with the same NumFmt class:
var NumFmt = require("<path-to-ilib>/lib/NumFmt.js");var fmt = new NumFmt({type: "percentage",maxFractionDigits: 2,locale: "tr-TR"});var percentString = fmt.format(0.893453);// % 89,34
Units
Various unit date is locale sensitive:Temperature, Speed, etc
Unit are formatted with the UnitFmt class.
If you see more in detail, Please visit the here.
Locale | Temperature | Vehicle Distance | Vehicle Speed | Fuel Volume |
---|---|---|---|---|
en-US | 87.8°F | 30 mi | 30 mph | 30 gal |
ko-KR | 31°C | 42.28 km | 42.28 km/h | 113.56L |
de-DE | 31 °C | 42.28 km | 42.28 km/h | 113.56 l |
Automatic Conversion
var MeasurementFactory = require("<path-to-ilib>/lib/MeasurementFactory")var UnitFmt = require("<path-to-ilib>/lib/UnitFmt");var m1 = MeasurementFactory({unit:"celsius", amount:31});var uf = new UnitFmt({locale:"en-US", length:"short", autoConvert:true});uf.format(m1)// '87.8°F'
Automatic Scaling
var m1 = MeasurementFactory({unit: "hectare", amount: 100});var uf = new UnitFmt({locale:"en-US", autoScale:true, autoConvert:false});var str = uf.format(m1);// 1 square kilometer