Parse a human readable time string into a time based value.
- Simple
TimeStringUtil objTimeString = new TimeStringUtil();
Double dblTotalSeconds = objTimeString.Parse("1d 6h 30m 15s");
// dblTotalSeconds = 109815
- Complex
TimeStringUtil objTimeString = new TimeStringUtil();
Double dblTotalSeconds = objTimeString.Parse("1y 2mth 4w 7d 12h 30m 15s 1000ms");
// dblTotalSeconds = 39886216
- Messy
TimeStringUtil objTimeString = new TimeStringUtil();
Double dblTotalSeconds = objTimeString.Parse("9 d 18hrs 27 mIn 3 6seC 1000 milli ");
// dblTotalSeconds = 844057
- Parse to TimeSpan
TimeStringUtil objTimeString = new TimeStringUtil();
TimeSpan tsNineDays = objTimeString.ParseToTimeSpan("9d");
// tsNineDays = new TimeSpan(9, 0, 0, 0)
- Parse to DateTime
TimeStringUtil objTimeString = new TimeStringUtil();
DateTime dtEighteenDays = objTimeString.ParseToDateTime("18d");
// dtEighteenDays.DayOfWeek = DateTime.Now.AddDays(18).DayOfWeek
- Custom Args
DEFAULT_OPTS objArgs = new DEFAULT_OPTS() {
hoursPerDay = 24,
daysPerWeek = 7,
weeksPerMonth = 4,
monthsPerYear = 12,
daysPerYear = 365.25,
};
TimeStringUtil objTimeString = new TimeStringUtil(objArgs);
Double dblTotalSeconds = objTimeString.Parse("1y");
// dblTotalSeconds = 31557600
All other examples are available here.
Please see the license file for more information.
- This repo is a .NET Port of timestring