Skip to content

Parse a human readable time string into a time based value.

License

Notifications You must be signed in to change notification settings

jadiagaurang/TimeString

Repository files navigation

Compile and Deploy to NuGet Gallery NuGet

TimeString

Parse a human readable time string into a time based value.

Usage

  1. Simple
TimeStringUtil objTimeString = new TimeStringUtil();
Double dblTotalSeconds = objTimeString.Parse("1d 6h 30m 15s");

// dblTotalSeconds = 109815
  1. Complex
TimeStringUtil objTimeString = new TimeStringUtil();
Double dblTotalSeconds = objTimeString.Parse("1y 2mth 4w 7d 12h 30m 15s 1000ms");

// dblTotalSeconds = 39886216
  1. Messy
TimeStringUtil objTimeString = new TimeStringUtil();
Double dblTotalSeconds = objTimeString.Parse("9 d  18hrs   27    mIn     3      6seC       1000        milli         ");

// dblTotalSeconds = 844057
  1. Parse to TimeSpan
TimeStringUtil objTimeString = new TimeStringUtil();
TimeSpan tsNineDays = objTimeString.ParseToTimeSpan("9d");

// tsNineDays = new TimeSpan(9, 0, 0, 0)
  1. Parse to DateTime
TimeStringUtil objTimeString = new TimeStringUtil();
DateTime dtEighteenDays = objTimeString.ParseToDateTime("18d");

// dtEighteenDays.DayOfWeek = DateTime.Now.AddDays(18).DayOfWeek
  1. 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.

License

Please see the license file for more information.

Credit