FirstDayOfMonth
public static DateTime FirstDayOfMonth(this DateTime dt)Returns a DateTime representing the first day of the month containing dt.
API documentation, members, signatures, and C# examples.
Find classes, members, and documentation.
DateTime extension methods provide utility functions for working with dates and times, particularly for trading calendars, option expiration dates, date formatting, and time-based strategy logic. Call these methods directly on a DateTime instance using standard extension method syntax.
public static DateTime FirstDayOfMonth(this DateTime dt)Returns a DateTime representing the first day of the month containing dt.
public static DateTime FloorToMinute(this DateTime dt)Returns a DateTime containing the same year, month, day, hour, and minute as dt, with seconds and milliseconds set to zero. The DateTime Kind is preserved.
public static int Get24HourTime(this DateTime dt)Returns the time portion of dt as a 24-hour HHmm integer. For example, 4:00 PM returns 1600.
public static int GetTime(this DateTime dt)Returns the time portion of dt as a 24-hour HHmm integer. Seconds are ignored. Examples
public static bool IsMaxValue(this DateTime dt)Returns true if dt represents, or is very close to, DateTime.MaxValue. The method considers any DateTime whose year is 9999 to be a maximum value.
public static bool IsMinValue(this DateTime dt)Returns true if dt represents, or is very close to, DateTime.MinValue. The method considers any DateTime whose year is 2 or earlier to be a minimum value.
public static int Quarter(this DateTime dt)Returns the zero-based calendar quarter containing dt. Possible values are:
public static DateTime RemoveSeconds(this DateTime dt)Returns a DateTime containing the same year, month, day, hour, and minute as dt, with the seconds set to zero.
public static int SemiAnnual(this DateTime dt)Returns the zero-based half-year containing dt. Returns 0 for January through June and 1 for July through December.
public static int CalendarDaysBetweenDates(this DateTime fromDate, DateTime toDate)public static int CalendarDaysBetweenDates(this int fromDate, int toDate)public static int CalendarDaysBetweenDates(this string fromDate, string toDate)Returns the number of calendar days between fromDate and toDate. The result is positive when toDate is later, zero when the dates are the same, and negative when toDate is earlier. When using the string overload, dates must be in yyyyMMdd format, for example:
int days = "20260801".CalendarDaysBetweenDates("20260826");
When using the integer overload, dates must also be expressed in yyyyMMdd format:
int days = 20260801.CalendarDaysBetweenDates(20260826);
public static string JSFormat(this DateTime dt)Returns dt formatted as a JavaScript-oriented date/time string using the form yyyy-MM-ddThh:mm:ssZ.
public static string ToISO8601(this DateTime dateTime)Converts dateTime to UTC and returns an ISO 8601-style date/time string.
public static string ToNewDateString(this DateTime dt)Returns C# source code representing the date portion of dt as a DateTime constructor. For example, a DateTime representing August 26, 2026 returns:
new DateTime(2026,8,26)
public static string ToShortDateTimeString(this DateTime dt, bool useSeconds = false)Returns dt using the current culture's short date format followed by a 24-hour time. By default, the time contains hours and minutes. Pass true for useSeconds to include seconds.
public static string ToSmartString(this DateTime dt)Returns a short date and time string if dt contains a non-zero time component. If the time is midnight, only the short date is returned.
public static bool IsOptionExpiry(this DateTime date)public static bool IsOptionExpiry(this DateTime date, BarHistory bars)Determines whether date is a monthly option expiration date. The parameterless version identifies the third Friday of the month and also accounts for U.S. stock market holidays that move expiration from Friday to Thursday. The BarHistory version uses the market calendar associated with bars to determine the applicable expiration date.
public static DateTime NextOptionExpiryDate(this DateTime date, BarHistory bars, bool useWeeklies = false)Returns the next option expiration date on or after date. By default, the method returns the next monthly expiration, normally the third Friday of the month. If the expiration Friday is a market holiday, the preceding trading day is returned. Pass true for useWeeklies to determine the next weekly expiration instead.
public static DateTime NthDayOfWeek(this DateTime dt, int nth, DayOfWeek dow)Returns the nth occurrence of the specified dow DayOfWeek within the month containing dt. For example, the following returns the third Friday of the month:
DateTime thirdFriday = dt.NthDayOfWeek(3, DayOfWeek.Friday);
public static int OptionsWeekOfMonth(this DateTime date)Returns the options week of the month, from 1 through 5. Options weeks are defined as Saturday through Friday, and a week belongs to the month containing its Friday.
public static int OptionsWeekOfYear(this DateTime date)Returns the options week of the year, from 1 through 53. Options weeks are defined as Saturday through Friday and belong to the year containing the week's Friday.
public static DateTime ThirdFriday(DateTime tempDate)Returns the date of the third Friday in the month containing tempDate.
public static DateTime GetNextTradingDate(this DateTime dt, BarHistory bars)public static DateTime GetNextTradingDate(this DateTime dt, MarketDetails mkt)Returns the next trading date after dt, based on the market associated with bars, or the specified mkt. Market trading days and holidays are taken into account. If dt contains a time component, it is preserved in the returned value.
public static bool IsATradingDay(this DateTime dt, BarHistory bars)Returns true if the market associated with bars trades on the date represented by dt. Holidays and the market's configured trading days of the week are taken into account. The time portion of dt is ignored.
public static bool IsHoliday(this DateTime dt, BarHistory bars)Returns true if the date represented by dt is contained in the holiday dates of the market associated with bars.
public static bool IsLastTradingDayOfMonth(this DateTime dt, BarHistory bars)Returns true if dt represents the last trading day of a calendar month for the market associated with bars.
public static bool IsLastTradingDayOfWeek(this DateTime dt, BarHistory bars)Returns true if dt represents the last trading day of a calendar week for the market associated with bars.
public static bool IsTradingDay(this DateTime dt, BarHistory bars)Returns true if the market associated with bars trades on the date represented by dt. Holidays and the market's configured trading days of the week are taken into account. The time portion of dt is ignored.
public static bool IsWeekend(this DateTime dt)Returns true if dt falls on a Saturday or Sunday.
public static DateTime PreviousTradingDay(this DateTime dt, MarketDetails md)Returns the trading day immediately preceding dt, based on the specified md MarketDetails instance. If dt contains a time component, it is preserved.
public static int TradingDayOfMonth(this DateTime dt, BarHistory bars)Returns the trading-day number within the month for dt, based on the market associated with bars.
public static int TradingHourOfDay(this DateTime dt, BarHistory bars)Returns the trading-hour number for dt based on the market hours associated with bars. Returns zero when dt is outside regular market hours.
public static long ToUnixDateTime(this DateTime dt)Returns the number of milliseconds between January 1, 1970 and dt.
public static DateTime UnixDateToDateTime(this long unixDate, bool inputInMS = false)Converts a Unix timestamp to a UTC DateTime. By default, unixDate is interpreted as seconds since the Unix epoch. Pass true for inputInMS if the value is already expressed in milliseconds.
public static bool IsBiweeklyStart(this DateTime dt)Returns true if dt falls in the first week of the class's alternating two-week cycle. The cycle is anchored to January 1, 1900. The supplied date is assumed to represent the first trading day of a week, allowing a Tuesday following a Monday holiday to be treated as belonging to that Monday's week.
public static int WeekOfYear(this DateTime dte)Returns the calendar week number of dte, from 1 through 52 or 53. Week 1 is defined as the first full Sunday-through-Saturday week in January. Consequently, dates at the beginning of January can belong to the final week of the preceding year's week sequence.
Ask the community or explore custom development services.