Show / Hide Table of Contents

Class DateTimeExtensions

Useful extension methods for System.DateTime.

Inheritance
Object
DateTimeExtensions
Inherited Members
System.Object.ToString()
Object.Equals(Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
Object.MemberwiseClone()
Namespace: IRM
Assembly: IRM.dll
Syntax
public static class DateTimeExtensions

Methods

AddWeekdays(DateTime, Int32)

Returns a new System.DateTime that adds the specified number of weekdays to the value of this instance.

Declaration
public static DateTime AddWeekdays(this DateTime date, int days)
Parameters
System.DateTime date

System.Int32 days

A specific number of days. Note that the number in the val parameter can be either positive or negative, and it express whole days.

Returns
System.DateTime

A new System.DateTime object having a value that represents the sum of the current DateTime object and the specified number of weekdays.

AddWorkDays(DateTime, Int32)

Returns a new DateTime that adds the specified number of work days to the value of this instance of DateTime.

Declaration
public static DateTime AddWorkDays(this DateTime dateTime, int value)
Parameters
System.DateTime dateTime

System.Int32 value

A number of whole work days.

Returns
System.DateTime

A new DateTime that adds the specified number of work days to the value of this instance

Remarks

The value parameter can be negative or positive>

FirstDayInMonth(DateTime)

Returns the first day in a month.

Declaration
public static DateTime FirstDayInMonth(this DateTime date)
Parameters
System.DateTime date

The date from which to return the first day in the month.

Returns
System.DateTime

A System.DateTime with the first day in the month.

Holidays(DateTime)

Returns a Dictionary<TKey,TValue> including the dates and the swedish name of all holidays of the year of this instance.

Declaration
public static Dictionary<DateTime, string> Holidays(this DateTime from)
Parameters
System.DateTime from

Returns
Dictionary<System.DateTime, String>

A Dictionary<TKey,TValue> including the dates and the swedish name of all holidays of the year of this instance.

Holidays(DateTime, DateTime)

Returns a Dictionary<TKey,TValue> including the dates and the swedish name of all holidays of the year of this instance to the year of to.

Declaration
public static Dictionary<DateTime, string> Holidays(this DateTime from, DateTime to)
Parameters
System.DateTime from

System.DateTime to

Returns
Dictionary<System.DateTime, String>

A Dictionary<TKey,TValue> including the dates and the swedish name of all holidays of the year of this instance to the year of to.

IsWorkDay(DateTime)

Indicates whether this instance of DateTime is a work day.

Declaration
public static bool IsWorkDay(this DateTime dateTime)
Parameters
System.DateTime dateTime

Returns
Boolean

A boolean value indicating whether this instance of DateTime is a work day.

LastDayInMonth(DateTime)

Returns the last day in a month.

Declaration
public static DateTime LastDayInMonth(this DateTime date)
Parameters
System.DateTime date

The date from which to return the last day in the month.

Returns
System.DateTime

A System.DateTime with the last day in the month.

Max(DateTime, DateTime)

Returns the latest of two dates.

Declaration
public static DateTime Max(DateTime val1, DateTime val2)
Parameters
System.DateTime val1

The first of two dates to compare.

System.DateTime val2

The second of two dates to compare.

Returns
System.DateTime

Parameter val1 or val2, whichever is latest.

Min(DateTime, DateTime)

Returns the earliest of two dates.

Declaration
public static DateTime Min(DateTime val1, DateTime val2)
Parameters
System.DateTime val1

The first of two dates to compare.

System.DateTime val2

The second of two dates to compare.

Returns
System.DateTime

Parameter val1 or val2, whichever is earliest.

NextDay(DateTime)

Returns the next day.

Declaration
public static DateTime NextDay(this DateTime date)
Parameters
System.DateTime date

The date from which to return the next day.

Returns
System.DateTime

A System.DateTime with next day.

ToLocalTime(DateTime, TimeZoneInfo)

Converts a UTC time to a time in the given time zone

Declaration
public static DateTime ToLocalTime(this DateTime utcTime, TimeZoneInfo targetTimeZone)
Parameters
System.DateTime utcTime

The UTC time

TimeZoneInfo targetTimeZone

The time zone to convert to

Returns
System.DateTime

ToLongDateString(DateTime)

Converts the value of the current System.DateTime object to its equivalent long date string representation.

Declaration
public static string ToLongDateString(this DateTime dateTime)
Parameters
System.DateTime dateTime

Returns
String

A string that contains the long date string representation of the current System.DateTime object.

ToLongDateString(DateTimeOffset)

Converts the value of the current System.DateTimeOffset object to its equivalent long date string representation.

Declaration
public static string ToLongDateString(this DateTimeOffset dateTime)
Parameters
System.DateTimeOffset dateTime

Returns
String

A string that contains the long date string representation of the current System.DateTimeOffset object.

ToLongTimeString(DateTime)

Converts the value of the current System.DateTime object to its equivalent long time string representation.

Declaration
public static string ToLongTimeString(this DateTime dateTime)
Parameters
System.DateTime dateTime

Returns
String

A string that contains the long time string representation of the current System.DateTime object.

ToLongTimeString(DateTimeOffset)

Converts the value of the current System.DateTimeOffset object to its equivalent long time string representation.

Declaration
public static string ToLongTimeString(this DateTimeOffset dateTime)
Parameters
System.DateTimeOffset dateTime

Returns
String

A string that contains the long time string representation of the current System.DateTimeOffset object.

ToRelativeDateString(DateTime)

Creates a string representing the relative time between date and System.DateTime.Now.

Declaration
public static string ToRelativeDateString(this DateTime date)
Parameters
System.DateTime date

Returns
String

ToRelativeDateStringUtc(DateTime)

Creates a string representing the relative time between date and System.DateTime.UtcNow.

Declaration
public static string ToRelativeDateStringUtc(this DateTime date)
Parameters
System.DateTime date

Returns
String

ToShortDateAndTimeOfDayString(DateTime)

Creates a string representing the date and the time of day from date.

Declaration
public static string ToShortDateAndTimeOfDayString(this DateTime date)
Parameters
System.DateTime date

Returns
String

A string from date representing the date and the time of day.

ToShortDateString(DateTime)

Converts the value of the current System.DateTime object to its equivalent short date string representation.

Declaration
public static string ToShortDateString(this DateTime dateTime)
Parameters
System.DateTime dateTime

Returns
String

A string that contains the short date string representation of the current System.DateTime object.

ToShortDateString(DateTimeOffset)

Converts the value of the current System.DateTimeOffset object to its equivalent short date string representation.

Declaration
public static string ToShortDateString(this DateTimeOffset dateTime)
Parameters
System.DateTimeOffset dateTime

Returns
String

A string that contains the short date string representation of the current System.DateTimeOffset object.

ToShortTimeString(DateTime)

Converts the value of the current System.DateTime object to its equivalent short time string representation.

Declaration
public static string ToShortTimeString(this DateTime dateTime)
Parameters
System.DateTime dateTime

Returns
String

A string that contains the short time string representation of the current System.DateTime object.

ToShortTimeString(DateTimeOffset)

Converts the value of the current System.DateTimeOffset object to its equivalent short time string representation.

Declaration
public static string ToShortTimeString(this DateTimeOffset dateTime)
Parameters
System.DateTimeOffset dateTime

Returns
String

A string that contains the short time string representation of the current System.DateTimeOffset object.

ToSwedishTime(DateTime)

Converts a UTC time to a time in Sweden.

Declaration
public static DateTime ToSwedishTime(this DateTime utcTime)
Parameters
System.DateTime utcTime

The UTC time

Returns
System.DateTime

ToUniversalTime(DateTime, TimeZoneInfo)

Converts a local time to a UTC time

Declaration
public static DateTime ToUniversalTime(this DateTime localTime, TimeZoneInfo sourceTimeZone)
Parameters
System.DateTime localTime

The local time

TimeZoneInfo sourceTimeZone

The time zone of the local time

Returns
System.DateTime

ToUnixTimeStamp(DateTime)

Returns a date converted to a Unix timestamp.

Declaration
public static long ToUnixTimeStamp(this DateTime dateTimeUtc)
Parameters
System.DateTime dateTimeUtc

The date to convert to a Unix timestamp.

Returns
System.Int64

The number of seconds that have elapsed since 00:00:00 UTC Thursday, 1 January 1970.

WorkDaysFrom(DateTime, DateTime)

Calculates the number of workdays between this instance of DateTime and another DateTime.

Declaration
public static int WorkDaysFrom(this DateTime datum, DateTime value)
Parameters
System.DateTime datum

System.DateTime value

Returns
System.Int32

Back to top Generated by DocFX