format
object exposes methods for formatting numbers, dates and durations to be human-readable.String
format(Number|Date value)
format.number()
. Otherwise, returns the value formatted with format.date()
.
value |
The value to format.
|
cli.tell(format(8.9)); // “8.90”
cli.tell(format(new Date('June 29, 2007 09:42'))); // “07-06-29 09:42:00”
String
format.number(Number value, [String|Array unit, [Number decimalPlaces]])
format.number.float()
.
String
format.number.float(Number value, [String|Array unit, [Number decimalPlaces]])
value |
The number to format.
|
unit |
The unit to append to the formatted number. Can either be an array of two strings (the singular and plural form respectively) or a single string, to which an “s” will be appended to create the plural form.
Defaults to null .
|
decimalPlaces |
The number of decimal places to include in the result. Zeroes are added to pad if necessary.
Defaults to 2 .
|
cli.tell(format.number(1, 'carrot')); // “1.00 carrot”
cli.tell(format.number(4528.5, 'carrot')); // “4,528.50 carrots”
cli.tell(format.number(12.536, ['cactus', 'cacti'])); // “12.54 cacti”
String
format.number.integer(Number value, [String|Array unit])
value |
The number to format.
|
unit |
The unit to append to the formatted number. See
Defaults to format.number.float for details.null .
|
cli.tell(format.number.integer(67.82, 'donut')); // “68 donuts”
String
format.date(Date|Moment value, [Boolean omitTime])
format.date.short()
.
String
format.date.short(Date|Moment value, [Boolean omitTime])
YY-MM-DD HH:mm:ss
format.value |
The date to format.
|
omitTime |
Whether or not to leave out the time component of the date.
Defaults to false .
|
cli.tell(format.date.short(new Date('March 11, 1984, 8:30'))); // “84-03-11 08:30:00”
String
format.date.long(Date|Moment value, [Boolean omitTime])
WeekDayName, MonthName MonthDay, FullYear HH:mm:ss
format.value |
The date to format.
|
omitTime |
Whether or not to leave out the time component of the date.
Defaults to false .
|
cli.tell(format.date.long(new Date('March 11, 1984, 8:30'))); // “Sunday, March 11th, 1984, 08:30:00”
String
format.date.relative(Date|Moment value)
"in 3 hours"
or "10 days ago"
.value |
The date to format.
|
const logDate = here.file('log.txt').dateModified;
cli.tell(format.date.relative(logDate)); // “3 minutes ago”
String
format.duration(Date|Moment date1, Date|Moment date2)
format.duration.between()
.
String
format.duration.between(Date|Moment date1, Date|Moment date2)
date1 |
The start of the duration.
|
date2 |
The end of the duration.
|
const startDate = new Date();
performSlowOperation();
const endDate = new Date();
cli.tell(format.duration(startDate, endDate)); // “32 minutes”