A powerful Go package that can parse dates and times from strings in over 150+ different formats. Perfect for log analysis, data processing, and any application that needs to extract temporal information from text.
- 150+ Date/Time Formats: Supports ISO 8601, RFC formats, Unix timestamps, relative dates, multilingual months, and more
- Intelligent Detection: Automatically finds and extracts dates from within larger text strings
- Date Removal: Option to remove the detected date from the input string
- Multiple Output Formats: JSON or plain text output
- Timezone Support: Handles various timezone formats and offsets
- Multilingual: Recognizes month names in English, Spanish, French, German, Italian, and Portuguese
go get github.com/yourusername/datelinego build -o dateline ./cmd/dateline# Parse a date from stdin
echo "Meeting on 2024-03-15 at 2:30 PM" | ./dateline
# Output:
# Parsed Date/Time:
# Time: 2024-03-15 00:00:00 UTC
# Unix: 1710460800
# Parsed: '2024-03-15'
# Position: [11:21]-remove: Remove the parsed date from the input string-json: Output results in JSON format-all: Find all dates in the input (not just the first)-v: Verbose output (show additional details)-h: Show help message
# Remove date from string
echo "File created on March 15, 2024 needs review" | ./dateline -remove
# Output: Without date: 'File created on needs review'
# JSON output
echo "2024-03-15T14:30:45Z" | ./dateline -json
# Output: {"time":"2024-03-15T14:30:45Z","unix":1710513045,...}
# Find all dates
echo "From 2024-01-01 to 2024-12-31" | ./dateline -all
# Output: Found 2 date(s)...
# Verbose mode
echo "today" | ./dateline -v
# Shows additional details like format type and ISO 8601 representationpackage main
import (
"fmt"
"log"
"dateline"
)
func main() {
// Simple parsing
input := "The meeting is scheduled for March 15, 2024 at 2:30 PM"
result, err := dateline.Parse(input, false)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found date: %s\n", result.Time.Format("2006-01-02 15:04:05"))
fmt.Printf("Original text: %s\n", result.OriginalText)
fmt.Printf("Parsed portion: %s\n", result.ParsedString)
fmt.Printf("Position: [%d:%d]\n", result.StartIndex, result.EndIndex)
}// Parse and remove the date
input := "Error occurred on 2024-03-15 - please investigate"
result, err := dateline.Parse(input, true) // true = remove date
if err != nil {
log.Fatal(err)
}
fmt.Printf("Text without date: %s\n", result.StringWithout)
// Output: "Error occurred on - please investigate"input := "Project runs from 2024-01-01 to 2024-12-31"
results, err := dateline.FindAllDates(input)
if err != nil {
log.Fatal(err)
}
for i, result := range results {
fmt.Printf("Date %d: %s at position [%d:%d]\n",
i+1, result.Time.Format("2006-01-02"),
result.StartIndex, result.EndIndex)
}import "time"
// Parse with specific timezone
nyLocation, _ := time.LoadLocation("America/New_York")
result, err := dateline.ParseWithOptions(
"Meeting at 3pm tomorrow",
dateline.WithLocation(nyLocation),
dateline.WithDateRemoval(true),
)2024-03-15T14:30:45Z2024-03-15T14:30:45+00:002024-03-15T14:30:45.12345678920240315T143045Z2024-03-15 14:30:45
Fri, 15 Mar 2024 14:30:45 GMT(RFC 1123)Friday, 15-Mar-24 14:30:45 GMT(RFC 850)2024-03-15T14:30:45Z(RFC 3339)
03/15/2024(American)15/03/2024(European)2024/03/15(Asian)15.03.2024(German)15-03-2024(ISO-like)
March 15, 202415 March 2024Mar 15, 202415th March 2024March 1st, 2024
15 mars 2024(French)15 marzo 2024(Spanish)15 de marzo de 2024(Spanish with "de")15. März 2024(German) *- Various other European languages
1710510645(seconds)1710510645123(milliseconds)@1710510645(with @ prefix)
today,yesterday,tomorrow2 days ago,in 3 hourslast Monday,next Friday
[15/Mar/2024:14:30:45 +0000](Log format)(2024-03-15T14:30:45Z)(Parentheses wrapped)20240315_143045(Underscore separated)- Database formats (Oracle, MySQL, PostgreSQL)
2024-W11(ISO week dates)Q1 2024(Quarter formats)Jan 2, 2006 at 3:04pm (MST)(Go reference format)
- Some multilingual formats with special characters
- Complex spelled-out dates
- Asian character formats (年月日, 년월일)
- Arabic/Persian numerals
- Julian dates
- Buddhist/Islamic calendar dates
- Some ordinal day formats (2024-074)
- Duration formats (P3Y6M4D)
Current parser performance on comprehensive format test suite:
Testing All Formats from formats.md
=====================================
✓ Passed: 155 / 224
✗ Failed: 69 / 224
Success Rate: 69%
Progress: [████████████████████████████████░░░░░░░░░░░░░░░░] 69%
| Category | Support Level | Examples |
|---|---|---|
| ISO 8601 | 95% | 2024-03-15T14:30:45Z |
| RFC Formats | 90% | Fri, 15 Mar 2024 14:30:45 GMT |
| American Dates | 100% | 03/15/2024, March 15, 2024 |
| European Dates | 85% | 15/03/2024, 15.03.2024 |
| Unix Timestamps | 100% | 1710510645 |
| Relative Dates | 100% | today, 2 days ago |
| Technical/Log | 80% | [15/Mar/2024:14:30:45 +0000] |
| Multilingual | 75% | 15 marzo 2024 |
| Quarter/Week | 60% | Q1 2024, 2024-W11 |
# Run comprehensive format test
./test-all-formats.sh
# Run basic test suite
./test.sh
# Test specific format
echo "your date string" | ./datelinetype ParseResult struct {
Time time.Time // Parsed time value
Format string // Format identifier used
OriginalText string // Original input string
StartIndex int // Start position of date in string
EndIndex int // End position of date in string
ParsedString string // The actual string that was parsed
StringWithout string // Input string with date removed
}Parses a date from the input string. If removeDate is true, removes the date from the string.
Finds all dates in the input string.
Parses with additional options like timezone and removal settings.
WithDateRemoval(bool)- Set whether to remove the date from inputWithLocation(*time.Location)- Set the timezone for parsing
Contributions are welcome! Areas for improvement:
- Additional language support for month names
- More Asian date formats
- Historical calendar systems
- Additional timezone abbreviations
MIT License - See LICENSE file for details
Built to handle the comprehensive date format list from real-world applications, supporting the most commonly encountered formats in logs, databases, and international systems.