Moment.js-based time
This tutorial shows you how to create a custom time cell type using Moment.js for validation and format auto-correction.
/* file: app.component.ts */import { Component } from '@angular/core';import { GridSettings, HotTableModule } from '@handsontable/angular-wrapper';import moment from 'moment';import { getRenderer } from 'handsontable/renderers';import { getEditor } from 'handsontable/editors';import { registerCellType } from 'handsontable/cellTypes';
const cellTimeTypeDefinition = { CELL_TYPE: 'moment-time', renderer: getRenderer('text'), validator(this: any, value: any, callback: (valid: boolean) => void): void { const timeFormat = this.timeFormat ?? 'h:mm:ss a'; let valid = true;
if (value === null) { value = ''; }
value = /^\d{3,}$/.test(value) ? parseInt(value, 10) : value;
const twoDigitValue = /^\d{1,2}$/.test(value);
if (twoDigitValue) { value += ':00'; }
const date = moment( value, ['YYYY-MM-DDTHH:mm:ss.SSSZ', 'X', 'x'], true ).isValid() ? moment(value) : moment(value, timeFormat);
let isValidTime = date.isValid(); let isValidFormat = moment(value, timeFormat, true).isValid() && !twoDigitValue;
if (this.allowEmpty && value === '') { isValidTime = true; isValidFormat = true; }
if (!isValidTime) { valid = false; }
if (!isValidTime && isValidFormat) { valid = true; }
if (isValidTime && !isValidFormat) { if (this.correctFormat === true) { const correctedValue = date.format(timeFormat);
this.instance.setDataAtCell(this.visualRow, this.visualCol, correctedValue, 'timeValidator'); valid = true; } else { valid = false; } }
callback(valid); }, editor: getEditor('text'),};
registerCellType('moment-time', cellTimeTypeDefinition);
@Component({ standalone: true, imports: [HotTableModule], selector: 'example1-moment-time', template: `<div><hot-table [data]="data" [settings]="gridSettings"></hot-table></div>`,})export class AppComponent { readonly data = [ { itemName: 'Lunar Core', category: 'Lander', leadEngineer: 'Ellen Ripley', time: '09:30', cost: 350000, }, { itemName: 'Zero Thrusters', category: 'Propulsion', leadEngineer: 'Sam Bell', time: '14:15', cost: 450000, }, { itemName: 'EVA Suits', category: 'Equipment', leadEngineer: 'Alex Rogan', time: '08:00', cost: 150000, }, { itemName: 'Solar Panels', category: 'Energy', leadEngineer: 'Dave Bowman', time: '16:45', cost: 75000, }, { itemName: 'Comm Array', category: 'Communication', leadEngineer: 'Louise Banks', time: '11:20', cost: 125000, }, { itemName: 'Habitat Dome', category: 'Shelter', leadEngineer: 'Dr. Ryan Stone', time: '23:00', cost: 1000000, }, ];
readonly gridSettings: GridSettings = { colHeaders: ['Item Name', 'Category', 'Lead Engineer', 'Arrival Time', 'Cost'], autoRowSize: true, rowHeaders: true, height: 'auto', width: '100%', autoWrapRow: true, headerClassName: 'htLeft', columns: [ { data: 'itemName', type: 'text', width: 130 }, { data: 'category', type: 'text', width: 120 }, { data: 'leadEngineer', type: 'text', width: 150 }, { data: 'time', type: 'moment-time' as any, width: 150, timeFormat: 'HH:mm', correctFormat: true, } as any, { data: 'cost', type: 'numeric', width: 120, className: 'htRight', locale: 'en-US', numericFormat: { style: 'currency', currency: 'USD', minimumFractionDigits: 2, }, }, ], };}/* end-file */
/* file: app.config.ts */import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';import { registerAllModules } from 'handsontable/registry';import { HOT_GLOBAL_CONFIG, HotGlobalConfig, NON_COMMERCIAL_LICENSE } from '@handsontable/angular-wrapper';
registerAllModules();
export const appConfig: ApplicationConfig = { providers: [ provideZoneChangeDetection({ eventCoalescing: true }), { provide: HOT_GLOBAL_CONFIG, useValue: { license: NON_COMMERCIAL_LICENSE } as HotGlobalConfig, }, ],};/* end-file */<div> <example1-moment-time></example1-moment-time></div>Overview
This guide shows how to create a custom time cell type using the Moment.js library. Users can format times using the Moment.js API.
Difficulty: Beginner
Time: ~15 minutes
Libraries: moment
What You’ll Build
A cell that:
- Displays time values as formatted text
- Accepts
timeFormatoptions for customization (e.g.,HH:mm,h:mm:ss a) - Validates time input using Moment.js
- Auto-corrects time format when
correctFormatis enabled
Prerequisites
npm install momentImport Dependencies
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';import { getRenderer } from 'handsontable/renderers';import { getEditor } from 'handsontable/editors';import { registerCellType } from 'handsontable/cellTypes';import moment from 'moment';registerAllModules();Why this matters:
momenthandles time parsing, validation, and formattinggetRenderer('text')andgetEditor('text')reuse Handsontable’s built-in text renderer and editorregisterCellTyperegisters the custom cell type for use in column config
Create the Renderer
The example reuses the built-in
textrenderer, which displays the time value as plain text:renderer: getRenderer('text')Create the Validator
The validator parses the input using Moment.js and checks it against the configured
timeFormat. It handles Unix timestamps, two-digit shorthand (e.g.,9becomes9:00), and auto-correction:validator: function(value, callback) {const timeFormat = this.timeFormat ?? 'h:mm:ss a';let valid = true;if (value === null) {value = '';}value = /^\d{3,}$/.test(value) ? parseInt(value, 10) : value;const twoDigitValue = /^\d{1,2}$/.test(value);if (twoDigitValue) {value += ':00';}const date = moment(value, ['YYYY-MM-DDTHH:mm:ss.SSSZ','X', // Unix timestamp'x' // Unix ms timestamp], true).isValid() ?moment(value) : moment(value, timeFormat);let isValidTime = date.isValid();// is it in the specified formatlet isValidFormat = moment(value, timeFormat, true).isValid() && !twoDigitValue;if (this.allowEmpty && value === '') {isValidTime = true;isValidFormat = true;}if (!isValidTime) {valid = false;}if (!isValidTime && isValidFormat) {valid = true;}if (isValidTime && !isValidFormat) {if (this.correctFormat === true) {const correctedValue = date.format(timeFormat);this.instance.setDataAtCell(this.visualRow, this.visualCol, correctedValue, 'timeValidator');valid = true;} else {valid = false;}}callback(valid);}What’s happening:
- Converts numeric-only input (3+ digits) to integers for Unix timestamp parsing
- Appends
:00to 1-2 digit values (e.g.,9becomes9:00) - Tries ISO 8601 and Unix timestamp formats first, then falls back to the configured
timeFormat - If
correctFormatis enabled, auto-corrects valid but misformatted times
Create the Editor
The example reuses the built-in
texteditor — a text input for editing the time value:editor: getEditor('text')Complete Cell Type Definition
Put all the pieces together and register the cell type:
const cellTimeTypeDefinition = {renderer: getRenderer('text'),validator: function(value, callback) {// ... validator code from Step 3},editor: getEditor('text'),};registerCellType('moment-time', cellTimeTypeDefinition);What’s happening:
- renderer: Uses the built-in
textrenderer to display the time value - validator: Custom validator that validates and optionally corrects time format using Moment.js
- editor: Uses the built-in
texteditor for text input - registerCellType: Registers the
moment-timecell type for use in column config
- renderer: Uses the built-in
Use in Handsontable
registerCellType('moment-time', cellTimeTypeDefinition);const hotOptions: Handsontable.GridSettings = {data,colHeaders: ['Item Name', 'Category', 'Lead Engineer', 'Arrival Time', 'Cost'],autoRowSize: true,rowHeaders: true,height: 'auto',width: '100%',autoWrapRow: true,headerClassName: 'htLeft',columns: [{ data: 'itemName', type: 'text', width: 130 },{ data: 'category', type: 'text', width: 120 },{ data: 'leadEngineer', type: 'text', width: 150 },{data: 'time',type: 'moment-time',width: 150,timeFormat: 'HH:mm',correctFormat: true,},{data: 'cost',type: 'numeric',width: 120,className: 'htRight',locale: 'en-US',numericFormat: {style: 'currency',currency: 'USD',minimumFractionDigits: 2,},},],licenseKey: 'non-commercial-and-evaluation',};const hot = new Handsontable(container, hotOptions);Key configuration:
type: 'moment-time'- uses the custom cell type on the Arrival Time columntimeFormat: 'HH:mm'- the Moment.js format string for 24-hour timecorrectFormat: true- automatically reformats valid times to the expected formatheaderClassName: 'htLeft'- left-aligns all column headers
How It Works - Complete Flow
- Initial Render: Cell displays the time value as plain text using the
textrenderer - User clicks cell: The built-in text editor opens for editing
- User enters time: Input like
9,14:30, or a Unix timestamp is accepted - Validation: Moment.js checks the format and time validity; auto-corrects if
correctFormatis enabled - Save: Valid values are saved to the cell; invalid values are rejected
What you learned
You created a custom Moment.js-based time cell type in Handsontable. You used Moment.js to validate and auto-correct time values, reused the built-in text renderer and editor, and registered the result with registerCellType for use across columns.
Next steps
- Moment.js date - The same Moment.js pattern applied to date values, with a Pikaday calendar picker.
- Numbro - A custom numeric cell type using the Numbro formatting library.
- Flatpickr - A date picker using Flatpickr with dark theme support.