DEV Community

cdilorenzo
cdilorenzo

Posted on

⏱️ Stop Parsing Durations by Hand — Meet SmartTimeSpanParser for .NET 9

Tired of writing boilerplate code to parse time durations like "2h 30m" or "1.5 days" into TimeSpan objects?
You're not alone. That's why I created:

👉 SmartTimeSpanParser

A small but powerful .NET library that converts natural language time durations into native TimeSpan values.


💡 Why This Library?

We’ve all been there:

TimeSpan.FromHours(2.5); // Cool. But what if the input is "2h 30m"?

Parsing text inputs from config files, CLI args, or user interfaces usually leads to ugly regex and a bunch of if-else spaghetti. With SmartTimeSpanParser, you write:

var span = SmartTimeSpan.Parse("1h 15m"); // ⏱️ 01:15:00

And you’re done.


🚀 Features

✅ Parses common formats like:

2h, 30m, 1.5 days, 90s

1h 30m, 2d 3h

✅ Returns native TimeSpan

✅ Supports TryParse

✅ Zero dependencies — just plug & play

✅ Targeting .NET 9 💪


📦 Install via NuGet

dotnet add package SmartTimeSpanParser


✅ Example Usage

var duration = SmartTimeSpan.Parse("2d 4h 15m");

Console.WriteLine(duration); // 52:15:00

Need to validate input safely?

if (SmartTimeSpan.TryParse("45 minutes", out var result))
{
Console.WriteLine(result); // 00:45:00
}


🛠️ What's Next?

Coming soon:

ToHumanString() → Turn TimeSpan back into "2h 30m"

Localization support (de, fr, es)

CLI tool

Milliseconds support


🤝 Contributors Welcome

Have a cool idea? Found a missing edge case?
Open a PR or an issue here:
👉 https://github.com/cdilorenzo/SmartTimeSpanParser


🧪 Fully Tested

With full test coverage using xUnit, you can trust it in production.


⭐ Support the Project

If you find this library useful:

➡️ Give it a ⭐ on GitHub
➡️ Share it with your developer friends
➡️ Use it in your next project!


Let me know what you think — I’d love feedback or ideas for future improvements!

Top comments (0)