moment-guess
raw JSON → 1.2.4 verified Fri May 01 auth: no javascript
A utility package for guessing a date string's format and outputting it as either moment.js format tokens or strftime format tokens. Current version 1.2.4. It relies on moment.js and date-and-time parsers to tokenize input, then applies refiners and assigners to produce the best guess. Useful for developers who need to reverse-engineer a date format from an example string. Unlike other date detection libraries, it supports moment.js and strftime output and handles ambiguous input by returning all possible matches. The package is maintained but has low release cadence.
Common errors
error TypeError: guessFormat is not a function ↓
cause Attempting to use ES module import (import guessFormat from 'moment-guess') without proper transpilation or in CommonJS environment.
fix
Use CommonJS require: const guessFormat = require('moment-guess');
error Couldn't parse date ↓
cause Input date string does not match any recognized pattern.
fix
Ensure the date string follows supported formats (ISO 8601, RFC 2822, slash/dot/dash separated, month-name variants).
error Cannot read property 'default' of undefined ↓
cause Using import guessFormat from 'moment-guess' in TypeScript with default export not properly resolved.
fix
Use import * as guessFormat from 'moment-guess' or const guessFormat = require('moment-guess').default;
Warnings
gotcha The function returns an array when input is ambiguous (e.g., '01/01/2020' for both US and UK formats). Do not assume a single string return. ↓
fix Always handle the result as either a string or an array of strings.
gotcha Input that cannot be parsed at all throws an error with message 'Couldn't parse date'. This includes completely invalid strings. ↓
fix Wrap calls in try-catch or validate input beforehand.
gotcha The 'strftime' format may throw 'Couldn't find strftime modifier for <token>' for unsupported tokens like 'Mo'. ↓
fix Check the list of supported tokens in documentation or use default format as fallback.
Install
npm install moment-guess yarn add moment-guess pnpm add moment-guess Imports
- default wrong
import guessFormat from 'moment-guess'correctconst guessFormat = require('moment-guess') - guessFormat wrong
const { guessFormat } = require('moment-guess')correctconst guessFormat = require('moment-guess') - guessFormat wrong
const guessFormat = require('moment-guess')correctconst guessFormat = require('moment-guess').default ?? require('moment-guess')
Quickstart
const guessFormat = require('moment-guess');
// Default moment format
try {
console.log(guessFormat('31/12/2020')); // 'DD/MM/YYYY'
} catch (err) {
console.error(err.message);
}
// Strftime format
try {
console.log(guessFormat('Fri, January 30th 2020, 10:00 AM', 'strftime'));
// '%a, %B %o %Y, %I:%M %p'
} catch (err) {
console.error(err.message);
}
// Ambiguous input returns array
try {
console.log(guessFormat('01/01/2020')); // ['DD/MM/YYYY', 'MM/DD/YYYY']
} catch (err) {
console.error(err.message);
}