本文整理汇总了C#中this.AddHours方法的典型用法代码示例。如果您正苦于以下问题:C# this.AddHours方法的具体用法?C# this.AddHours怎么用?C# this.AddHours使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.AddHours方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AM
public static DateTime AM(this DateTime d)
{
Reject24HourTime(d);
if (d.Hour < 12)
return d;
return d.AddHours(-12);
}
示例2: Interval
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;
}
示例3: NextHour
///////////////////////////////////////////////////////////////////////
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;
}
示例4: ToChineseTime
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;
}
示例5: DateFromTimeString
/// <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;
}
示例6: ToISO8601
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");
}
示例7: ConvertToUserDate
/// <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);
}
示例8: DateTimeShift
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;
}
示例9: RoundMinutes
/// <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");
}
}
示例10: Add
/// <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;
}
示例11: SubstractTimeStepUnit
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;
}
示例12: ToUtc
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();
}
示例13: SubtractHours
/// <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());
}
示例14: AddAnHour
/// <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);
}
示例15: SubtractHours
public static DateTime SubtractHours(this DateTime dateTime, double hours)
{
return dateTime.AddHours(-hours);
}