Class DateTimeExtension
Extensions for DateTime.
Inheritance
Inherited Members
Namespace: StoneCo.Framework.Extension
Assembly: cs.temp.dll.dll
Syntax
public static class DateTimeExtension
Methods
Format(DateTime, Int32)
Formats a 'DateTime' object into its string representation.
Declaration
public static string Format(this DateTime value, int decimalPlaces = 3)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | value | 'DateTime' object to be formatted. |
System.Int32 | decimalPlaces | Number of digits to the right of the decimal point. |
Returns
Type | Description |
---|---|
System.String | String representation of the 'DateTime' object. |
FormatIso(DateTime, Int32)
Formats a 'DateTime' object into its string representation.
Declaration
public static string FormatIso(this DateTime value, int decimalPlaces = 3)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | value | Value for 'this'. |
System.Int32 | decimalPlaces | Number of digits to the right of the decimal point. |
Returns
Type | Description |
---|---|
System.String | String representation of the 'DateTime' object. |
FormatIso(DateTimeOffset, Int32)
Formats a 'DateTimeOffset' object into its string representation.
Declaration
public static string FormatIso(this DateTimeOffset value, int decimalPlaces = 3)
Parameters
Type | Name | Description |
---|---|---|
System.DateTimeOffset | value | Value for 'this'. |
System.Int32 | decimalPlaces | Number of digits to the right of the decimal point. |
Returns
Type | Description |
---|---|
System.String | String representation of the 'DateTimeOffset' object. |
FormatWithOffset(DateTimeOffset, Int32)
Formats a 'DateTimeOffset' object into its string representation.
Declaration
public static string FormatWithOffset(this DateTimeOffset value, int decimalPlaces = 3)
Parameters
Type | Name | Description |
---|---|---|
System.DateTimeOffset | value | 'DateTimeOffset' object to be formatted. |
System.Int32 | decimalPlaces | Number of digits to the right of the decimal point. |
Returns
Type | Description |
---|---|
System.String | String representation of the 'DateTimeOffset' object. |
IsFirstDayOfMonth(DateTime)
Check if it's the first day of the month.
Declaration
public static bool IsFirstDayOfMonth(this DateTime date)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to check. |
Returns
Type | Description |
---|---|
System.Boolean | Value true or false |
Examples
var date = DateTime.Now;
var isFirstDayOfMonth = data.IsFirstDayOfMonth();
IsLastDayOfMonth(DateTime)
Check if it's the last day of the month.
Declaration
public static bool IsLastDayOfMonth(this DateTime date)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to check. |
Returns
Type | Description |
---|---|
System.Boolean | Value true or false |
Examples
var date = DateTime.Now;
var isLastDayOfMonth = data.IsLastDayOfMonth();
ToAge(DateTime, Nullable<DateTime>)
Get age
Declaration
public static int ToAge(this DateTime dateBirth, DateTime? dateToCalculate = default(DateTime? ))
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | dateBirth | Date of birth |
System.Nullable<System.DateTime> | dateToCalculate | Date to calculate age. If the date is not entered, the current date will be used. |
Returns
Type | Description |
---|---|
System.Int32 | Age |
Examples
Calcule age and pass date to calculate .
var dateOfBirth = new DateTime(1983, 3, 3);
var dateToCalculate = new DateTime(2012, 12, 21);
var age = dateOfBirth.ToAge(dateToCalculate);
Calcule age and not pass date to calculate .
var dateOfBirth = new DateTime(1983, 3, 3);
var age = dateOfBirth.ToAge();
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | If the |
ToDateWithLastTime(DateTime)
Get date with last time. Time format 23:59:59 999.
Declaration
public static DateTime ToDateWithLastTime(this DateTime date)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to get last time. |
Returns
Type | Description |
---|---|
System.DateTime | Date with last time |
Examples
var date = DateTime.Now;
var dateWithLastTime = data.ToDateWithLastTime();
ToDaysBetweenDifferentDates(DateTime, DateTime)
Get days between two dates.
Declaration
public static int ToDaysBetweenDifferentDates(this DateTime date, DateTime dateToCompare)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Lowest value date. |
System.DateTime | dateToCompare | Highest value date. |
Returns
Type | Description |
---|---|
System.Int32 | Total days. |
Examples
var date = DateTime.Now;
var dateToCompare = date.AddDays(5);
var days = date.ToDaysBetweenDifferentDates(dateToCompare);
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | If the |
ToDaysToEndOfMonth(DateTime, Boolean)
Get total days until the end of the month.
Declaration
public static int ToDaysToEndOfMonth(this DateTime date, bool includeCurrentDay = false)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to get total days. |
System.Boolean | includeCurrentDay | Include current day into total. |
Returns
Type | Description |
---|---|
System.Int32 | Total days. |
Examples
Total days until the end of the month and excluding current day.
var date = DateTime.Now;
var daysUntilEndOfMonth = date.ToDaysToEndOfMonth();
Total days until the end of the month and including current day.
var date = DateTime.Now;
var includeCurrentDay = true;
var daysUntilEndOfMonth = date.ToDaysToEndOfMonth(includeCurrentDay);
ToDaysToEndOfYear(DateTime, Boolean)
Get total days until the end of the year.
Declaration
public static int ToDaysToEndOfYear(this DateTime date, bool includeCurrentDay = false)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to get total days. |
System.Boolean | includeCurrentDay | Include current day into total. |
Returns
Type | Description |
---|---|
System.Int32 | Total days. |
Examples
Total days until the end of the year and excluding current day.
var date = DateTime.Now;
var daysUntilEndOfYear = date.ToDaysToEndOfYear();
Total days until the end of the year and including current day.
var date = DateTime.Now;
var includeCurrentDay = true;
var daysUntilEndOfYear = date.ToDaysToEndOfYear(includeCurrentDay);
ToFirstDayOfMonth(DateTime)
Get first day of month.
Declaration
public static DateTime ToFirstDayOfMonth(this DateTime date)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to get first day of month. |
Returns
Type | Description |
---|---|
System.DateTime | First day of month. |
Examples
var date = DateTime.Now;
var firstDayOfmonth = date.ToFirstDayOfMonth();
ToLastDayOfMonth(DateTime)
Get last day of month.
Declaration
public static DateTime ToLastDayOfMonth(this DateTime date)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Date to get last day of month. |
Returns
Type | Description |
---|---|
System.DateTime | Last day of month. |
Examples
var date = DateTime.Now;
var lastDayOfmonth = date.ToLastDayOfMonth();
ToMonthsBetweenDifferentDates(DateTime, DateTime)
Get months between two dates.
Declaration
public static int ToMonthsBetweenDifferentDates(this DateTime date, DateTime dateToCompare)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Lowest value date. |
System.DateTime | dateToCompare | Highest value date. |
Returns
Type | Description |
---|---|
System.Int32 | Total months. |
Examples
var date = DateTime.Now;
var dateToCompare = date.AddMonths(5);
var months = date.ToMonthsBetweenDifferentDates(dateToCompare);
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | If the |
ToYearsBetweenDifferentDates(DateTime, DateTime)
Get years between two dates.
Declaration
public static int ToYearsBetweenDifferentDates(this DateTime date, DateTime dateToCompare)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | Lowest value date. |
System.DateTime | dateToCompare | Highest value date. |
Returns
Type | Description |
---|---|
System.Int32 | Total years. |
Examples
var date = DateTime.Now;
var dateToCompare = date.AddMonths(5);
var years = date.ToYearsBetweenDifferentDates(dateToCompare);
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | If the |