本文整理汇总了C#中DateTime.Subtract方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.Subtract方法的具体用法?C# DateTime.Subtract怎么用?C# DateTime.Subtract使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime.Subtract方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RoundUp
public static TimeSpan RoundUp(this DateTime dateTime, int minute = 10)
{
var roundUpTime = new DateTime(dateTime.Year, dateTime.Month,
dateTime.Day, dateTime.Hour, ((dateTime.Minute / minute) * minute), 0).AddMinutes(minute);
return roundUpTime.Subtract(DateTime.Now);
}
示例2: InstantToRelative
public static double InstantToRelative(DateTime instant, DateTime start, TimeUnits units = TimeUnits.SECONDS)
{
TimeSpan tspan;
double instantAsRelative = 0.0;
tspan = instant.Subtract(start);
//Years and Months are not exactly
switch (units)
{
case TimeUnits.YEARS:
instantAsRelative = tspan.TotalDays / 365.0;
break;
case TimeUnits.MONTHS:
instantAsRelative = tspan.TotalDays / 30.0;
break;
case TimeUnits.DAYS:
instantAsRelative = tspan.TotalDays;
break;
case TimeUnits.HOURS:
instantAsRelative = tspan.TotalHours;
break;
case TimeUnits.MINUTES:
instantAsRelative = tspan.TotalMinutes;
break;
case TimeUnits.SECONDS:
instantAsRelative = tspan.TotalSeconds;
break;
}
return instantAsRelative;
}
示例3: FrequencyParameter_NoArgPassed_ReportingPeriod
public void FrequencyParameter_NoArgPassed_ReportingPeriod()
{
FrequencyParameter testParameter = new FrequencyParameter();
DateTime fromResult = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
DateTime thruResult = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
thruResult = thruResult.Add(new TimeSpan(1, 0, 0, 0));
TimeSpan expectedResult = thruResult.Subtract(fromResult);
Assert.AreEqual(expectedResult, testParameter.ReportingPeriod);
}
示例4: SetToDateTime2
internal void SetToDateTime2(DateTime dateTime, byte scale)
{
Debug.Assert(IsEmpty, "setting value a second time?");
_type = StorageType.DateTime2;
_value._dateTime2Info.timeInfo.ticks = dateTime.TimeOfDay.Ticks;
_value._dateTime2Info.timeInfo.scale = scale;
_value._dateTime2Info.date = dateTime.Subtract(DateTime.MinValue).Days;
_isNull = false;
}
示例5: SetToDate
internal void SetToDate(DateTime date)
{
Debug.Assert(IsEmpty, "setting value a second time?");
_type = StorageType.Date;
_value._int32 = date.Subtract(DateTime.MinValue).Days;
_isNull = false;
}
示例6: WriteDate
private void WriteDate(DateTime value, TdsParserStateObject stateObj)
{
long days = value.Subtract(DateTime.MinValue).Days;
WritePartialLong(days, 3, stateObj);
}
示例7: OnDecrement
/// <summary>
/// Subtracts a time span from a date time in a global context.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="timeSpan">The time span.</param>
/// <returns>The DateTime after decrementing by TimeSpan.</returns>
public virtual DateTime OnDecrement(DateTime value, TimeSpan timeSpan)
{
// special case: value is at DateTime.MinValue
if (DateTime.MinValue.Date == value.Date && value.TimeOfDay < timeSpan)
{
return value.AddDays(1).Subtract(timeSpan);
}
return value.Subtract(timeSpan);
}
示例8: GetDaysFromZenith
int GetDaysFromZenith (DateTime date)
{
TimeSpan span = date.Subtract (dateZenith);
return span.Days;
}
示例9: SetDateTime
// valid for DateTime, SmallDateTime, Date, DateTime2
internal void SetDateTime(DateTime value)
{
Debug.Assert(
SmiXetterAccessMap.IsSetterAccessValid(_metaData, SmiXetterTypeCode.XetDateTime));
if (SqlDbType.Variant == _metaData.SqlDbType)
{
if ((_variantType != null) && (_variantType.SqlDbType == SqlDbType.DateTime2))
{
_stateObj.Parser.WriteSqlVariantDateTime2(value, _stateObj);
}
else if ((_variantType != null) && (_variantType.SqlDbType == SqlDbType.Date))
{
_stateObj.Parser.WriteSqlVariantDate(value, _stateObj);
}
else
{
TdsDateTime dt = MetaType.FromDateTime(value, 8);
_stateObj.Parser.WriteSqlVariantHeader(10, TdsEnums.SQLDATETIME, 0, _stateObj);
_stateObj.Parser.WriteInt(dt.days, _stateObj);
_stateObj.Parser.WriteInt(dt.time, _stateObj);
}
// Clean the variant metadata to prevent sharing it with next row.
// As a reminder, SetVariantType raises an assert if _variantType is not clean
_variantType = null;
}
else
{
_stateObj.WriteByte((byte)_metaData.MaxLength);
if (SqlDbType.SmallDateTime == _metaData.SqlDbType)
{
TdsDateTime dt = MetaType.FromDateTime(value, (byte)_metaData.MaxLength);
Debug.Assert(0 <= dt.days && dt.days <= UInt16.MaxValue, "Invalid DateTime '" + value + "' for SmallDateTime");
_stateObj.Parser.WriteShort(dt.days, _stateObj);
_stateObj.Parser.WriteShort(dt.time, _stateObj);
}
else if (SqlDbType.DateTime == _metaData.SqlDbType)
{
TdsDateTime dt = MetaType.FromDateTime(value, (byte)_metaData.MaxLength);
_stateObj.Parser.WriteInt(dt.days, _stateObj);
_stateObj.Parser.WriteInt(dt.time, _stateObj);
}
else
{ // date and datetime2
int days = value.Subtract(DateTime.MinValue).Days;
if (SqlDbType.DateTime2 == _metaData.SqlDbType)
{
Int64 time = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[_metaData.Scale];
_stateObj.WriteByteArray(BitConverter.GetBytes(time), (int)_metaData.MaxLength - 3, 0);
}
_stateObj.WriteByteArray(BitConverter.GetBytes(days), 3, 0);
}
}
}
示例10: GetDayOfYear
/// <summary>
/// Returns the day of the year in the specified date.
/// </summary>
/// <param name="time">The <see cref="DateTime"/> to get the day of year for.</param>
/// <returns>The one-based day of the year.</returns>
public override int GetDayOfYear( DateTime time )
{
var year = time.Year - ( time.Month < epochMonth ? 1 : 0 );
var start = new DateTime( year, epochMonth, 1 );
return (int) Math.Ceiling( time.Subtract( start ).TotalDays ) + 1;
}
示例11: ConvertGregorianToHijri
/*========================ConvertGregorianToHijri============================
** Purpose: convert DateTime to Hdate(year,month,day)
** Arguments:
** Input: DateTime
** Output: Hijrah date: year:yh, month:mh, day:dh
============================================================================*/
private static void ConvertGregorianToHijri(DateTime time, ref int HijriYear, ref int HijriMonth, ref int HijriDay)
{
int index, b, DaysPerThisMonth;
double nDays;
TimeSpan ts;
int yh1 = 0, mh1 = 0, dh1 = 0;
Debug.Assert((time.Ticks >= minDate.Ticks) && (time.Ticks <= maxDate.Ticks), "Gregorian date is out of range.");
// Find the index where we should start our search by quessing the Hijri year that we will be in HijriYearInfo.
// A Hijri year is 354 or 355 days. Use 355 days so that we will search from a lower index.
index = (int)((time.Ticks - minDate.Ticks) / Calendar.TicksPerDay) / 355;
do
{
} while (time.CompareTo(s_hijriYearInfo[++index].GregorianDate) > 0); //while greater
if (time.CompareTo(s_hijriYearInfo[index].GregorianDate) != 0)
{
index--;
}
ts = time.Subtract(s_hijriYearInfo[index].GregorianDate);
yh1 = index + MinCalendarYear;
mh1 = 1;
dh1 = 1;
nDays = ts.TotalDays;
b = s_hijriYearInfo[index].HijriMonthsLengthFlags;
DaysPerThisMonth = 29 + (b & 1);
while (nDays >= DaysPerThisMonth)
{
nDays -= DaysPerThisMonth;
b = b >> 1;
DaysPerThisMonth = 29 + (b & 1);
mh1++;
}
dh1 += (int)nDays;
HijriDay = dh1;
HijriMonth = mh1;
HijriYear = yh1;
}
示例12: FromDateTime
private static SqlDateTime FromDateTime(DateTime value)
{
// DevNote: SqlDateTime has smaller precision and range than DateTime.
// Usually we round the DateTime value to the nearest SqlDateTime value.
// but for DateTime.MaxValue, if we round it up, it will overflow.
// Although the overflow would be the correct behavior, we simply
// returned SqlDateTime.MaxValue in v1. In order not to break exisiting
// code, we'll keep this logic.
//
if (value == DateTime.MaxValue)
return SqlDateTime.MaxValue;
return FromTimeSpan(value.Subtract(s_SQLBaseDate));
}