Table of Contents

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

dateTime DateTime

The DateTime to modify (passed by reference)

days double

The 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

birthDate DateTime

Birth date

dtMinVal DateTime

Minimum DateTime value

Returns

byte

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

bday DateTime

The birth date

dtMinVal DateTime

The 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

result DateTime

The DateTime providing the date part

time DateTime

The 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

year int

The year

month int

The month (1-12)

day int

The day (1-31)

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

year string

The year as string

month string

The month as string

day string

The day as string

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

day string

The day as string

month string

The month as string

hour string

The hour as string

minute string

The 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

hour string

The hour as string

minutes string

The minutes as string

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

dateTime DateTime

DateTime to convert

secondsInHour long

Seconds in one hour

Returns

long

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

yearByte byte

The 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

startDate DateTime

The start date (inclusive)

endDate DateTime

The end date (inclusive)

Returns

List<DateTime>

List of all dates in the range

HasNullableDateTimeValue(DateTime?)

Check also for MinValue and MaxValue

public static bool HasNullableDateTimeValue(DateTime? dateTime)

Parameters

dateTime DateTime?

Returns

bool

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

yearText string

The 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

text string

Returns

string

SecondsInMonth(DateTime)

Calculates the total number of seconds in the month of the specified DateTime.

public static long SecondsInMonth(DateTime dateTime)

Parameters

dateTime DateTime

The 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

year int

The 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

dateTime DateTime

The 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

dateTime DateTime

The original DateTime

hour int

The new hour value

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

dateTime DateTime

The original DateTime

minute int

The new minute value

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

ugtFirstStep DateTime

The 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

year int

The 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

dateTime DateTime

EN: The date to get the start of the week for. CZ: Datum pro které se má získat začátek týdne.

rolloverDayOfWeek DayOfWeek?

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

firstDateTime DateTime
secondDateTime DateTime

Returns

TimeSpan

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

parameter Stopwatch

The 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

dateTime DateTime

The 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

dateTime DateTime

The 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

dateTime DateTime

The DateTime to strip time from

Returns

DateTime

DateTime with time set to 00:00:00