dumbdateparser
v1.2.1
Published
A stupidly simple date parser - part of the DumbWare suite
Readme
DumbDateParser
Part of the DumbWare suite - Why So DUMB?
A stupidly simple date parser that just works. No fancy algorithms, no complex rules - just parse dates like a human would.
Installation
npm install dumbdateparserUsage
Node.js
import DumbDateParser from 'dumbdateparser';
// Use the static method
const date = DumbDateParser.parseDate('tomorrow');
// Or create an instance
const parser = new DumbDateParser();
const date = parser.parse('next friday');Browser
<script src="node_modules/dumbdateparser/src/browser.js"></script>
<script>
// The parser is available globally as DumbDateParser
const date = DumbDateParser.parseDate('tomorrow');
</script>Supported Date Formats
Relative dates:
todaytomorrowyesterdaynext weeknext monthnext year
Days of the week:
monday(ormon)tuesday(ortue,tues)wednesday(orwed,weds)thursday(orthu,thur,thurs)friday(orfri)saturday(orsat)sunday(orsun)
Next specific day:
next mondaynext friday- etc.
Ordinal dates:
1st of january15th march23rd of december
Simple dates:
mm/ddmm-ddmm.dd
Full dates:
yyyy/mm/ddyyyy-mm-ddyyyy.mm.dd
Time and Timezone Support
Time Formats
- 24-hour format:
15:00,9:30 - 12-hour format:
3pm,9:30am - Time of day words:
morning(5 AM by default)afternoon(12 PM by default)evening(5 PM by default)night(8 PM by default)
Timezone Support
The parser supports common timezone abbreviations and handles conversions automatically:
// EST input will be converted to your local timezone
const parser = new DumbDateParser();
const date = parser.parse('mar 2 at 2pm est');
// Set a default timezone for all parsing
const tzParser = new DumbDateParser({ defaultTimezone: 'pst' });
const date = tzParser.parse('tomorrow at 3pm'); // Will be interpreted as PSTSupported timezones:
- PST/PDT (Pacific Time)
- MST/MDT (Mountain Time)
- CST/CDT (Central Time)
- EST/EDT (Eastern Time)
- UTC/GMT
- BST (British Summer Time)
- CET (Central European Time)
- IST (Indian Standard Time)
If no timezone is specified, the parser assumes the input is in your local timezone.
Customizing Time Defaults
You can override the default times for morning, afternoon, evening, and night:
const parser = new DumbDateParser({
timeDefaults: {
morning: 7, // 7 AM
afternoon: 13, // 1 PM
evening: 18, // 6 PM
night: 22 // 10 PM
}
});You can also set these via environment variables:
DUMB_TIME_MORNINGDUMB_TIME_AFTERNOONDUMB_TIME_EVENINGDUMB_TIME_NIGHT
Examples with Time and Timezone
const parser = new DumbDateParser();
// Different time formats
parser.parse('tomorrow at 15:00'); // 24-hour format
parser.parse('friday at 3:30pm'); // 12-hour format
parser.parse('monday morning'); // Time of day word
// Timezone handling
parser.parse('mar 2 at 2pm est'); // EST converted to local time
parser.parse('tomorrow at 3pm pst'); // PST converted to local time
parser.parse('friday at 12pm utc'); // UTC converted to local time
// Default timezone
const tzParser = new DumbDateParser({ defaultTimezone: 'est' });
tzParser.parse('tomorrow at 3pm'); // Interpreted as 3 PM ESTWhy So Dumb?
At DumbWare, we believe in the power of stupid simple solutions. This date parser doesn't try to be clever - it just does what you'd expect. No fancy algorithms, no complex rules, just pure, unadulterated simplicity that somehow gets the job done.
License
MIT
Contributing
- Fork it
- Make it dumber
- Create a pull request
Disclaimer
This is part of the DumbWare suite - a collection of stupidly simple software that somehow gets the job done. Use at your own risk, and don't blame us when it parses "next christmas" as "null" because we were too lazy to implement holiday parsing.
