public static DateTime AM(this DateTime d) { Reject24HourTime(d); if (d.Hour < 12) return d; return d.AddHours(-12); }
public static DateTime Interval(this DateTime date, DateTimePart? intervalType, int? intervalVal) { if (intervalType.HasValue && intervalVal.HasValue) { switch (intervalType.Value) { case DateTimePart.Year: return date.AddYears(intervalVal.Value); case DateTimePart.Month: return date.AddMonths(intervalVal.Value); case DateTimePart.Day: return date.AddDays((double)intervalVal.Value); case DateTimePart.Hour: return date.AddHours((double)intervalVal.Value); case DateTimePart.Munite: return date.AddMinutes((double)intervalVal.Value); case DateTimePart.Second: return date.AddSeconds((double)intervalVal.Value); case DateTimePart.Week: return date.AddDays((double)intervalVal.Value * 7); case DateTimePart.Quarter: return date.AddMonths(intervalVal.Value * 3); } } return date; }
public static DateTime AddType(this DateTime instance, TimeUnits type, double increment) { DateTime result = instance; switch (type) { case TimeUnits.Days: result = instance.AddDays(increment); break; case TimeUnits.Months: increment *= DateTime.DaysInMonth(instance.Year, instance.Month); result = instance.AddDays((int)(Math.Floor(increment))); break; case TimeUnits.Weeks: result = instance.AddDays(increment * 7); break; case TimeUnits.Years: if (DateTime.IsLeapYear(instance.Year)) increment *= 366; else increment *= 365; result = instance.AddDays((int)(Math.Floor(increment))); break; case TimeUnits.Hours: result = instance.AddHours(increment); break; } return result; }
/////////////////////////////////////////////////////////////////////// public static DateTime NextHour(this DateTime date) { DateTime next = date.AddHours(1); next = next.SetMinute(0); next = next.SetSecond(0); next = next.SetMillisecond(0); return next; }
public static DateTime ToChineseTime(this DateTime dt) { //var cnOffset = TimeZoneInfo.FindSystemTimeZoneById("Asia/Shanghai").BaseUtcOffset; //var cnUTC = dt.Add(cnOffset); var cnUTC = dt.AddHours(8); var cnDT = new DateTime(cnUTC.Year, cnUTC.Month, cnUTC.Day, cnUTC.Hour, cnUTC.Minute, cnUTC.Second, cnUTC.Millisecond, DateTimeKind.Unspecified); return cnDT; }
/// <summary> /// Return a DateTime value /// </summary> /// <param name="dt">the current datetime</param> /// <param name="time">the time of day in 24-hour format (i.e. "21:30")</param> /// <returns></returns> public static DateTime DateFromTimeString(this DateTime dt, string time) { var parts = time.Split(':'); DateTime ret = dt.AddHours(parts[0].ToDouble()); if (parts.Length == 2) ret = ret.AddMinutes(parts[1].ToDouble()); return ret; }
public static string ToISO8601(this DateTime dt, double hourShift = 0) { string retValue = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); if (dt == null) return retValue; return dt.AddHours(hourShift).ToString("yyyy-MM-ddTHH:mm:ssZ"); }
/// <summary> /// 转换成用户所在时区的时间 /// </summary> /// <param name="dateTime">待转换日期(UTC时间)</param> /// <returns>返回用户所在时区的时间</returns> public static DateTime ConvertToUserDate(this DateTime dateTime) { //User currentUser; //DateTime userDate = dt.AddHours(currentUser.Timezone); if (dateTime.Kind == DateTimeKind.Local) return dateTime; else return dateTime.AddHours(8); }
public static int DateTimeShift(this DateTime dateTime) { int hour = dateTime.AddHours(-7).Hour; if (hour >= 0 & hour < 9) return 1; if (hour >= 9 & hour < 16) return 2; if (hour >= 16 & hour < 24) return 3; return 1; }
/// <summary> /// Rounds the minute component to the nearest specified time. Defaults to 1 hour /// </summary> /// <param name="source"></param> /// <param name="rounding"></param> /// <returns></returns> public static DateTime RoundMinutes(this DateTime source, MinuteRounding rounding = MinuteRounding.Hour) { if (rounding == MinuteRounding.QuarterHour) throw new NotImplementedException("Coming soon"); switch (rounding) { case MinuteRounding.Hour: if (source.Minute >= 30) return source.AddHours(1).AddMinutes(-source.Minute).TrimSeconds(); return source.AddMinutes(-source.Minute).TrimSeconds(); case MinuteRounding.HalfHour: if (source.Minute < 15) return source.AddMinutes(-source.Minute).TrimSeconds(); if (source.Minute >= 15 && source.Minute < 45) return source.AddMinutes(-source.Minute).AddMinutes(30).TrimSeconds(); return source.AddHours(1).AddMinutes(-source.Minute).TrimSeconds(); default: throw new ArgumentOutOfRangeException("rounding"); } }
/// <summary> /// Adds a generic AddType to a DateTime object /// </summary> /// <param name="now"><seealso cref="System.DateTime"/></param> /// <param name="adder">Type structure that acts as a switcher for what type of add to perform</param> /// <param name="much">How much AddType to add to each element for creating list of data</param> /// <returns>A DateTime object with the added AddType amounts</returns> public static DateTime Add(this DateTime now, AddType adder, double much) { DateTime ret = now; switch (adder) { case AddType.Years: { ret = now.AddYears((int)much); break; } case AddType.Months: { ret = now.AddMonths((int)much); break; } case AddType.Days: { ret = now.AddDays(much); break; } case AddType.Hours: { ret = now.AddHours(much); break; } case AddType.Minutes: { ret = now.AddMinutes(much); break; } case AddType.Seconds: { ret = now.AddSeconds(much); break; } case AddType.Milliseconds: { ret = now.AddMilliseconds(much); break; } case AddType.Ticks: { ret = now.AddTicks((long)much); break; } } return ret; }
public static DateTime SubstractTimeStepUnit(this DateTime dt, TimeStepUnit tstep) { switch (tstep) { case TimeStepUnit.Year: return dt.AddYears(-1); case TimeStepUnit.Month: return dt.AddMonths(-1); case TimeStepUnit.Day: return dt.AddDays(-1); case TimeStepUnit.Hour: return dt.AddHours(-1); case TimeStepUnit.Minute: return dt.AddMinutes(-1); case TimeStepUnit.Second: return dt.AddSeconds(-1); } return dt; }
public static DateTime ToUtc(this DateTime dateTime, TimeZoneInfo timeZoneInfo) { if (dateTime.Kind == DateTimeKind.Utc) return dateTime; if (timeZoneInfo == null) return dateTime.ToUniversalTime(); var offSet1 = new DateTimeOffset(dateTime); var utcDateTime = dateTime.ToUniversalTime(); var tzDate = utcDateTime.ToTimeZone(timeZoneInfo); var offSet2 = new DateTimeOffset(tzDate); var offSet3 = offSet1 - offSet2; var date = dateTime.AddHours(offSet3.Hours); return date.ToUniversalTime(); }
/// <summary> /// Subtracts given hours from the given DateTime. /// </summary> /// <param name="date">The given DateTime.</param> /// <param name="hours">Number of hours to be subtracted.</param> /// <returns>The DateTime after the given hours are subtracted.</returns> public static DateTime SubtractHours(this DateTime date, int hours) { return date.AddHours(hours.Negate()); }
/// <summary> /// Adds one hour to the given DateTime. /// </summary> /// <param name="date">The given DateTime.</param> /// <returns>The DateTime after one hour is added.</returns> public static DateTime AddAnHour(this DateTime date) { return date.AddHours(1); }
public static DateTime SubtractHours(this DateTime dateTime, double hours) { return dateTime.AddHours(-hours); }
public static DateTime SubtractHours(this DateTime date, double value) { if (value < 0) throw new ArgumentException("Value cannot be less than 0.", "value"); return date.AddHours(value * -1); }
/// <summary> /// Convert a UTC date time to a date time expressed in GMT or BST /// </summary> /// <param name="dateTime">The date and time in UTC</param> /// <returns>The date and time in either GMT or BST</returns> public static DateTime UTCToGMTBST(this DateTime dateTime) { DateTime theDate = dateTime; if (TimeZoneInfo.Local.IsDaylightSavingTime(theDate)) { theDate = dateTime.AddHours(1); } return theDate; }
/// <summary> /// Returns a DateTime a number of hours in the future. /// </summary> /// <param name="self">The DateTime to move</param> /// <param name="hours">Number of hours</param> public static DateTime HoursSince(this DateTime self, double hours) { return self.AddHours(hours); }
public static DateTime ChangeHour(this DateTime date, int hour) { if (hour < 0 || hour > 23) throw new ArgumentException("Value must be between 0 and 23.", "hour"); return date.AddHours(hour - date.Hour); }
/// <summary> /// Warning: bug found. dt.Offset may be incorrect as offsets could be different if range crosses daylight saving switch, i.e. October - December, or month of November in the EST USA. /// </summary> /// <param name="dt"></param> /// <param name="count"></param> /// <param name="unit"></param> /// <returns></returns> public static DateTimeOffset Add(this DateTimeOffset dt, int count, TimeUnits unit) { switch(unit) { case TimeUnits.Century: return dt.AddYears(count*100); case TimeUnits.Day: return dt.AddDays(count); case TimeUnits.Decade: return dt.AddYears(count*10); case TimeUnits.Hour: return dt.AddHours(count); case TimeUnits.Minute: return dt.AddMinutes(count); case TimeUnits.Month: return dt.AddMonths(count); case TimeUnits.Quarter: return dt.AddMonths(count*3); case TimeUnits.Second: return dt.AddSeconds(count); case TimeUnits.Week: return dt.AddDays(count*7); case TimeUnits.Year: return dt.AddYears(count); } throw new Exception("Adding \"{0}\" is not implemented.".SmartFormat(unit)); }
public static DateTime ToOffsetDateTime(this DateTime date, double offset) { if (date.TimeOfDay.TotalSeconds > 0) return date.AddHours(offset); return date; }
public static DateTimeOffset SubtractHours(this DateTimeOffset dateTimeOffset, double hours) { return dateTimeOffset.AddHours(-hours); }
/// <summary> /// Return IST time assuming given date is UTC /// </summary> /// <param name="date">UTC Date</param> /// <returns></returns> public static DateTime ToIst(this DateTime date) { return date.AddHours(5).AddMinutes(30); }
public static DateTime BeginningOfDay(this DateTime date) { return date.AddHours(date.Hour * -1).AddMinutes(date.Minute * -1); }
public static IEnumerable<DateTime> GetHours(this DateTime date) { date = date.Date; // truncate hours for (int i = 0; i < 24; i++) { yield return date.AddHours(i); } }
/// <summary> /// Returns a DateTime a number of hours in the past /// </summary> /// <param name="self">The DateTime to move</param> /// <param name="hours">Number of hours</param> public static DateTime HoursAgo(this DateTime self, double hours) { return self.AddHours(-hours); }
/// <summary> /// 获取下一个交易分钟 /// </summary> /// <param name="self"></param> /// <returns></returns> public static DateTime ToNextTradingMinute(this DateTime self) { if (self.IsTradingDate() == false) { self = self.ToNextTradingDate(); return self.AddHours(9).AddMinutes(31); } DateTime time = self.AddMinutes(1); TimeSpan span = time.TimeOfDay; if (span < new TimeSpan(9, 31, 0)) { return time.Date.AddHours(9).AddMinutes(31); } else if (span > new TimeSpan(11, 30, 0) && span < new TimeSpan(13, 0, 0)) { return time.Date.AddHours(13).AddMinutes(1); } else if (span > new TimeSpan(15, 0, 0)) { return time.Date.AddDays(1).ToNextTradingMinute(); } return time; }
public static string ToiCalTime(this DateTime dateTime, int timeZoneDifference) { return dateTime.AddHours(timeZoneDifference).ToString("yyyyMMdd") + "T" + dateTime.AddHours(timeZoneDifference).ToString("HHmmssZ"); }
/////////////////////////////////////////////////////////////////////// public static DateTime SetHour(this DateTime dt, int hour) { if ((hour < 0) || (hour > 23)) { throw new ArgumentOutOfRangeException("hour"); } int diff = hour - dt.Hour; return dt.AddHours(diff); }