Class DTHelperGeneral
- Namespace
- SunamoDateTime.DT
- Assembly
- SunamoDateTime.dll
Provides general-purpose DateTime utility methods for date manipulation, creation, and conversion.
public class DTHelperGeneral
- Inheritance
-
DTHelperGeneral
- Inherited Members
- Extension Methods
Methods
AddDays(ref DateTime, double)
Adds the specified number of days to the DateTime reference and returns the result.
public static DateTime AddDays(ref DateTime dateTime, double days)
Parameters
dateTimeDateTimeThe DateTime to modify (passed by reference)
daysdoubleThe number of days to add (can be negative)
Returns
- DateTime
The modified DateTime
CalculateAge(DateTime, DateTime)
A2 = SqlServerHelper.DateTimeMinVal if A1 = A2, return 255
public static byte CalculateAge(DateTime birthDate, DateTime dtMinVal)
Parameters
Returns
CalculateAgeString(DateTime, DateTime)
Calculates age from a birth date and returns it as a string. Returns empty string for unset dates (255).
public static string CalculateAgeString(DateTime bday, DateTime dtMinVal)
Parameters
bdayDateTimeThe birth date
dtMinValDateTimeThe minimum DateTime value representing an unset date
Returns
- string
Age as string, or empty string if date is unset
Combine(DateTime, DateTime)
Combines the date part of the first DateTime with the time part of the second DateTime.
public static DateTime Combine(DateTime result, DateTime time)
Parameters
resultDateTimeThe DateTime providing the date part
timeDateTimeThe DateTime providing the time part
Returns
- DateTime
Combined DateTime
Create(int, int, int)
Creates a DateTime from year, month and day integers. Returns null if creation fails.
public static DateTime? Create(int year, int month, int day)
Parameters
Returns
- DateTime?
Created DateTime or null if arguments are out of range
Create(string, string, string)
Creates a DateTime from year, month and day strings. Returns null if creation fails.
public static DateTime? Create(string year, string month, string day)
Parameters
Returns
- DateTime?
Created DateTime or null if arguments are out of range
Create(string, string, string, string)
Creates a DateTime with year 1 from day, month, hour and minute strings.
public static DateTime Create(string day, string month, string hour, string minute)
Parameters
daystringThe day as string
monthstringThe month as string
hourstringThe hour as string
minutestringThe minute as string
Returns
- DateTime
Created DateTime with year 1
CreateTime(string, string)
Creates a DateTime at MinValue date with the specified hour and minutes.
public static DateTime CreateTime(string hour, string minutes)
Parameters
Returns
- DateTime
DateTime.MinValue with added hours and minutes
DateTimeToSecondsOnlyTime(DateTime, long)
Is counting only time, return as non-normalized int
public static long DateTimeToSecondsOnlyTime(DateTime dateTime, long secondsInHour)
Parameters
Returns
FullYear(byte)
Converts a byte value to a four-digit year by prepending "2" (e.g. 24 becomes 2024).
public static int FullYear(byte yearByte)
Parameters
yearBytebyteThe year as a byte value
Returns
- int
Four-digit year as integer
GetDatesBetween(DateTime, DateTime)
Returns a list of all dates between the start and end dates, inclusive.
public static List<DateTime> GetDatesBetween(DateTime startDate, DateTime endDate)
Parameters
Returns
HasNullableDateTimeValue(DateTime?)
Check also for MinValue and MaxValue
public static bool HasNullableDateTimeValue(DateTime? dateTime)
Parameters
dateTimeDateTime?
Returns
LongYear(string)
Converts a two-digit year string to a four-digit year string. Years 0-79 map to 2000-2079, 80-99 map to 1980-1999.
public static string LongYear(string yearText)
Parameters
yearTextstringThe two-digit year string
Returns
- string
Four-digit year string
ParseYear(string)
Find four digit letter in any string
public static string ParseYear(string text)
Parameters
textstring
Returns
SecondsInMonth(DateTime)
Calculates the total number of seconds in the month of the specified DateTime.
public static long SecondsInMonth(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime whose month to calculate seconds for
Returns
- long
Total seconds in the month
SecondsInYear(int)
Calculates the total number of seconds in the specified year, accounting for leap years.
public static long SecondsInYear(int year)
Parameters
yearintThe year to calculate seconds for
Returns
- long
Total seconds in the year
SetDateToMinValue(DateTime)
Returns a new DateTime preserving the time component but setting the date to DateTime.MinValue.
public static DateTime SetDateToMinValue(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime whose time to preserve
Returns
- DateTime
DateTime with MinValue date and original time
SetHour(DateTime, int)
Returns a new DateTime with the hour component replaced by the specified value.
public static DateTime SetHour(DateTime dateTime, int hour)
Parameters
Returns
- DateTime
DateTime with the hour replaced
SetMinute(DateTime, int)
Returns a new DateTime with the minute component replaced by the specified value.
public static DateTime SetMinute(DateTime dateTime, int minute)
Parameters
Returns
- DateTime
DateTime with the minute replaced
SetToday(DateTime)
Returns a new DateTime with today's date and the time from the specified DateTime.
public static DateTime SetToday(DateTime ugtFirstStep)
Parameters
ugtFirstStepDateTimeThe DateTime whose time component to use
Returns
- DateTime
Today's date with the specified time
ShortToday()
Returns today's date as a DateTimeOrShort using the compact short representation.
public static DateTimeOrShort ShortToday()
Returns
- DateTimeOrShort
Today's date as DateTimeOrShort
ShortYear(int)
Returns the last two digits of a year (e.g. 2024 becomes "24").
public static string ShortYear(int year)
Parameters
yearintThe four-digit year
Returns
- string
Two-digit year string
StartOfWeekMonday(DateTime, DayOfWeek?)
EN: Gets the start of the week (Monday) for the specified date. CZ: Získá začátek týdne (pondělí) pro zadané datum.
public static DateTime StartOfWeekMonday(DateTime dateTime, DayOfWeek? rolloverDayOfWeek)
Parameters
dateTimeDateTimeEN: The date to get the start of the week for. CZ: Datum pro které se má získat začátek týdne.
rolloverDayOfWeekDayOfWeek?EN: Optional day of week threshold. If the current day is >= this day, returns the start of NEXT week instead of current week. CZ: Volitelný den v týdnu jako práh. Pokud je aktuální den >= tento den, vrátí začátek PŘÍŠTÍHO týdne místo aktuálního týdne.
Returns
- DateTime
EN: The Monday of the week (current or next based on threshold). CZ: Pondělí daného týdne (aktuálního nebo příštího podle prahu).
Substract(DateTime, DateTime)
Subtract A2 from A1
public static TimeSpan Substract(DateTime firstDateTime, DateTime secondDateTime)
Parameters
Returns
TimeInMsToSeconds(Stopwatch)
Converts elapsed milliseconds from a Stopwatch to a seconds string (e.g. "1.23s") and resets the Stopwatch.
public static string TimeInMsToSeconds(Stopwatch parameter)
Parameters
parameterStopwatchThe Stopwatch to read and reset
Returns
- string
Elapsed time in seconds as string with "s" suffix
TodayPlusActualHour()
Returns today's date with the current hour added.
public static DateTime TodayPlusActualHour()
Returns
- DateTime
Today at the current hour
WeekOfYearFromDate(DateTime)
Calculates the ISO 8601 week number for the specified date.
public static int WeekOfYearFromDate(DateTime dateTime)
Parameters
dateTimeDateTimeThe date to get the week number for
Returns
- int
ISO 8601 week number (1-53)
WithoutDate(DateTime)
Returns a new DateTime with only the time component, setting the date to year 1, January 1.
public static DateTime WithoutDate(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime to strip date from
Returns
- DateTime
DateTime with date set to 0001-01-01
WithoutTime(DateTime)
Returns a new DateTime with only the date component, zeroing the time part.
public static DateTime WithoutTime(DateTime dateTime)
Parameters
dateTimeDateTimeThe DateTime to strip time from
Returns
- DateTime
DateTime with time set to 00:00:00