本文整理汇总了C#中DateTimeOffset.AddMonths方法的典型用法代码示例。如果您正苦于以下问题:C# DateTimeOffset.AddMonths方法的具体用法?C# DateTimeOffset.AddMonths怎么用?C# DateTimeOffset.AddMonths使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeOffset
的用法示例。
在下文中一共展示了DateTimeOffset.AddMonths方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitCustomers
private static void InitCustomers()
{
DateTimeOffset dto = new DateTimeOffset(2015, 1, 1, 1, 2, 3, 4, TimeSpan.Zero);
_customers = Enumerable.Range(1, 5).Select(e =>
new DCustomer
{
Id = e,
DateTime = dto.AddYears(e).DateTime,
Offset = e % 2 == 0 ? dto.AddMonths(e) : dto.AddDays(e).AddMilliseconds(10),
Date = e % 2 == 0 ? dto.AddDays(e).Date : dto.AddDays(-e).Date,
TimeOfDay = e % 3 == 0 ? dto.AddHours(e).TimeOfDay : dto.AddHours(-e).AddMilliseconds(10).TimeOfDay,
NullableDateTime = e % 2 == 0 ? (DateTime?)null : dto.AddYears(e).DateTime,
NullableOffset = e % 3 == 0 ? (DateTimeOffset?)null : dto.AddMonths(e),
NullableDate = e % 2 == 0 ? (Date?)null : dto.AddDays(e).Date,
NullableTimeOfDay = e % 3 == 0 ? (TimeOfDay?)null : dto.AddHours(e).TimeOfDay,
DateTimes = new [] { dto.AddYears(e).DateTime, dto.AddMonths(e).DateTime },
Offsets = new [] { dto.AddMonths(e), dto.AddDays(e) },
Dates = new [] { (Date)dto.AddYears(e).Date, (Date)dto.AddMonths(e).Date },
TimeOfDays = new [] { (TimeOfDay)dto.AddHours(e).TimeOfDay, (TimeOfDay)dto.AddMinutes(e).TimeOfDay },
NullableDateTimes = new [] { dto.AddYears(e).DateTime, (DateTime?)null, dto.AddMonths(e).DateTime },
NullableOffsets = new [] { dto.AddMonths(e), (DateTimeOffset?)null, dto.AddDays(e) },
NullableDates = new [] { (Date)dto.AddYears(e).Date, (Date?)null, (Date)dto.AddMonths(e).Date },
NullableTimeOfDays = new [] { (TimeOfDay)dto.AddHours(e).TimeOfDay, (TimeOfDay?)null, (TimeOfDay)dto.AddMinutes(e).TimeOfDay },
}).ToList();
}
示例2: GetEvents
private static List<Event> GetEvents(DateTimeOffset may, DateTimeOffset june)
{
var inRange = new Event { StartDateTime = may, EndDateTime = june };
var startBeforeRangeEndsInRange = new Event { StartDateTime = may.AddMonths(-1), EndDateTime = june };
var startInRangeEndsAfterRange = new Event { StartDateTime = may, EndDateTime = june.AddMonths(1) };
var startsAndEndsAfterRange = new Event { StartDateTime = may.AddMonths(2), EndDateTime = june.AddMonths(2) };
var startsAndEndsBeforRange = new Event { StartDateTime = may.AddMonths(-2), EndDateTime = june.AddMonths(-2) };
var events = new List<Event>
{
startsAndEndsBeforRange,
startBeforeRangeEndsInRange,
inRange,
startInRangeEndsAfterRange,
startsAndEndsAfterRange,
};
return events;
}
示例3: CreateAbonnements
public void CreateAbonnements(int kundeId, DateTimeOffset start, DateTimeOffset stop)
{
var period = ((stop.Year - start.Year) * 12) + stop.Month - start.Month;
if (period == 0)
{
_context.Abonnements.Add(new Data.Abonnement(kundeId, start.Year, start.Month, true));
}
else
{
for (int i = 0; i < period; i++)
{
_context.Abonnements.Add(new Data.Abonnement(kundeId, start.AddMonths(i).Year, start.AddMonths(i).Month, true));
}
}
_context.SaveChanges();
}
示例4: GetSundaysInMonth
public static IEnumerable<DateTimeOffset> GetSundaysInMonth(this DateTimeOffset date)
{
DateTimeOffset start = new DateTimeOffset(date.Year, date.Month, 1, 0, 0, 0, DateTimeOffset.Now.Offset);
var sunday = date.GetSundayNearMonthsBegin();
while (sunday < start.AddMonths(1))
{
yield return sunday;
sunday = sunday.AddDays(7);
}
}
示例5: CanAddMonthsAcrossDstTransition
public void CanAddMonthsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dto = new DateTimeOffset(2015, 2, 9, 0, 0, 0, TimeSpan.FromHours(-8));
var result = dto.AddMonths(1, tz);
var expected = new DateTimeOffset(2015, 3, 9, 0, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}
示例6: ParseDayHourMinute
/// <summary>
/// Parses the time in day hour minute format..
/// </summary>
/// <param name="reference">The reference.</param>
/// <param name="dayHourMinute">The time string in day hour minute format.</param>
/// <returns>DateTimeOffset.</returns>
public static DateTimeOffset ParseDayHourMinute(DateTimeOffset reference, string dayHourMinute)
{
if (reference == DateTimeOffset.MinValue || reference == DateTimeOffset.MaxValue)
reference = DateTimeOffset.UtcNow;
int dayOfMonth, hour, minute;
int.TryParse(dayHourMinute.Substring(0, 2), out dayOfMonth);
int.TryParse(dayHourMinute.Substring(2, 2), out hour);
int.TryParse(dayHourMinute.Substring(4, 2), out minute);
if (dayOfMonth != reference.Day)
{
// Check for previous month
if (dayOfMonth > 25 && reference.Day < 5)
reference = reference.AddMonths(-1);
// Next month
else if (dayOfMonth < 5 && reference.Day > 25)
reference = reference.AddMonths(1);
}
return new DateTimeOffset(reference.Year, reference.Month, dayOfMonth, hour, minute, 0, TimeSpan.Zero);
}
示例7: TestDateTimeOffsetDayLightSavingWhenAddingMonth
public void TestDateTimeOffsetDayLightSavingWhenAddingMonth()
{
DateTimeOffset someNov2014 = new DateTime(2014, 11, 12, 22, 28, 11, 0, DateTimeKind.Local);
DateTimeOffset someDec2014 = someNov2014.AddMonths(1);
DateTimeOffset dec2014Start = new DateTimeOffset(someDec2014.Year, someDec2014.Month, 1, 0, 0, 0, someNov2014.Offset);
DateTimeOffset nov2014End = dec2014Start.PreviousMoment();
DateTimeOffset nov2014Start = dec2014Start.AddMonths(-1);
DateTimeOffset expectedStart = someNov2014.StartOf(TimeUnits.Month);
DateTimeOffset expectedEnd = someNov2014.EndOf(TimeUnits.Month);
Assert.AreEqual(expectedStart, nov2014Start);
Assert.AreEqual(expectedEnd, nov2014End);
}
示例8: TestAddition
public static void TestAddition()
{
DateTimeOffset dt = new DateTimeOffset(new DateTime(1986, 8, 15, 10, 20, 5, 70));
Assert.Equal(17, dt.AddDays(2).Day);
Assert.Equal(13, dt.AddDays(-2).Day);
Assert.Equal(10, dt.AddMonths(2).Month);
Assert.Equal(6, dt.AddMonths(-2).Month);
Assert.Equal(1996, dt.AddYears(10).Year);
Assert.Equal(1976, dt.AddYears(-10).Year);
Assert.Equal(13, dt.AddHours(3).Hour);
Assert.Equal(7, dt.AddHours(-3).Hour);
Assert.Equal(25, dt.AddMinutes(5).Minute);
Assert.Equal(15, dt.AddMinutes(-5).Minute);
Assert.Equal(35, dt.AddSeconds(30).Second);
Assert.Equal(2, dt.AddSeconds(-3).Second);
Assert.Equal(80, dt.AddMilliseconds(10).Millisecond);
Assert.Equal(60, dt.AddMilliseconds(-10).Millisecond);
}
示例9: From_FromFixedDateTime_Tests
public void From_FromFixedDateTime_Tests(int value)
{
var originalPointInTime = new DateTimeOffset(1976, 12, 31, 17, 0, 0, 0, TimeSpan.Zero);
Assert.AreEqual(value.Years().From(originalPointInTime), originalPointInTime.AddYears(value));
Assert.AreEqual(value.Months().From(originalPointInTime), originalPointInTime.AddMonths(value));
Assert.AreEqual(value.Weeks().From(originalPointInTime), originalPointInTime.AddDays(value*DaysPerWeek));
Assert.AreEqual(value.Days().From(originalPointInTime), originalPointInTime.AddDays(value));
Assert.AreEqual(value.Hours().From(originalPointInTime), originalPointInTime.AddHours(value));
Assert.AreEqual(value.Minutes().From(originalPointInTime), originalPointInTime.AddMinutes(value));
Assert.AreEqual(value.Seconds().From(originalPointInTime), originalPointInTime.AddSeconds(value));
Assert.AreEqual(value.Milliseconds().From(originalPointInTime), originalPointInTime.AddMilliseconds(value));
Assert.AreEqual(value.Ticks().From(originalPointInTime), originalPointInTime.AddTicks(value));
}
示例10: ResetByDate
internal void ResetByDate()
{
var rResetState = false;
var rNow = DateTimeOffset.Now.ToOffset(TimeZoneOffset);
if (!UpdateTime.HasValue)
Current = 0;
else
{
var rUpdateTime = UpdateTime.Value;
DateTimeOffset rResetTime;
switch (Type)
{
case QuestType.Daily:
case QuestType.Special1:
case QuestType.Special2:
rResetTime = new DateTimeOffset(rNow.Year, rNow.Month, rNow.Day, 5, 0, 0, TimeZoneOffset);
if (rNow.Hour < 5)
rResetTime -= TimeSpan.FromDays(1.0);
rResetState = rUpdateTime < rResetTime;
break;
case QuestType.Weekly:
var rOffset = rNow.DayOfWeek - DayOfWeek.Monday;
if (rOffset < 0)
rOffset += 7;
rResetTime = rNow.AddDays(-1 * rOffset) - rNow.TimeOfDay + ResetHour;
rResetState = rUpdateTime < rResetTime;
break;
case QuestType.Monthly:
rResetTime = new DateTimeOffset(rNow.Year, rNow.Month, 1, 5, 0, 0, TimeZoneOffset);
if (rNow.Hour < 5)
rResetTime = rResetTime.AddMonths(-1);
rResetState = rUpdateTime < rResetTime;
break;
}
}
if (rResetState)
{
DebugUtil.Log(string.Format("任务 {0}: 被重置 原时间={1}", ID, UpdateTime.Value));
StateInternal = QuestState.None;
CurrentInternal = 0;
UpdateTime = rNow;
RecordManager.Instance.Quest.UpdateProgress(this);
RecordManager.Instance.Quest.UpdateStatus(this);
}
}
示例11: When_a_value_is_not_one_of_the_specified_values_it_should_throw_with_descriptive_message
public void When_a_value_is_not_one_of_the_specified_values_it_should_throw_with_descriptive_message()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
DateTimeOffset value = new DateTimeOffset(31.December(2016), 1.Hours());
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action action = () => value.Should().BeOneOf(new[] { value.AddDays(1), value.AddMonths(1) }, "because it's true");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
action.ShouldThrow<AssertFailedException>()
.WithMessage("Expected value to be one of {<2017-01-01 +1h>, <2017-01-31 +1h>} because it's true, but found <2016-12-31 +1h>.");
}
示例12: CreateMonthRange
/// <summary>
/// Creates the month range.
/// </summary>
/// <param name="current">The current</param>
/// <returns>Set of days</returns>
public static DateRange CreateMonthRange(DateTimeOffset current)
{
DateTimeOffset start = new DateTimeOffset(current.Year, current.Month, 1, 0, 0, 0, 0, TimeSpan.Zero);
DateTimeOffset end = start.AddMonths(1).AddMilliseconds(-1);
return new DateRange(start, end);
}
示例13: ResetDataSource
public IHttpActionResult ResetDataSource()
{
DateAndTimeOfDayContext db = new DateAndTimeOfDayContext();
if (!db.Customers.Any())
{
DateTimeOffset dateTime = new DateTimeOffset(2014, 12, 24, 1, 2, 3, 4, new TimeSpan(-8, 0, 0));
IEnumerable<EfCustomer> customers = Enumerable.Range(1, 5).Select(e =>
new EfCustomer
{
Id = e,
DateTime = dateTime.AddYears(e).AddHours(e).AddMilliseconds(e).DateTime,
NullableDateTime = e % 2 == 0 ? (DateTime?)null : dateTime.AddHours(e * 5).AddMilliseconds(e * 5).DateTime,
Offset = dateTime.AddMonths(e).AddHours(e).AddMilliseconds(e),
NullableOffset = e % 3 == 0 ? (DateTimeOffset?)null : dateTime.AddDays(e).AddHours(e * 5)
}).ToList();
foreach (EfCustomer customer in customers)
{
db.Customers.Add(customer);
}
db.SaveChanges();
}
return Ok();
}
示例14: ListLogs
//.........这里部分代码省略.........
yield return log;
}
}
else
{
yield break;
}
}
dateCounter = dateCounter.AddHours(1);
if (dateCounter > DateTimeOffset.UtcNow.AddHours(1))
{
yield break;
}
}
// Check logs using a day-based prefix until we reach a month boundary.
while (dateCounter.Day > 1)
{
string currentPrefix = logDirectory.Prefix + dateCounter.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
IEnumerable<IListBlobItem> currentLogs = logDirectory.Container.ListBlobs(currentPrefix, true, metadataDetails, options, operationContext);
foreach (ICloudBlob log in currentLogs)
{
if (!utcEndTime.HasValue || string.Compare(log.Parent.Prefix, endPrefix) <= 0)
{
if (IsCorrectLogType(log, operations))
{
yield return log;
}
}
else
{
yield break;
}
}
dateCounter = dateCounter.AddDays(1);
if (dateCounter > DateTimeOffset.UtcNow.AddHours(1))
{
yield break;
}
}
// Check logs using a month-based prefix until we reach a year boundary.
while (dateCounter.Month > 1)
{
string currentPrefix = logDirectory.Prefix + dateCounter.ToString("yyyy/MM", CultureInfo.InvariantCulture);
IEnumerable<IListBlobItem> currentLogs = logDirectory.Container.ListBlobs(currentPrefix, true, metadataDetails, options, operationContext);
foreach (ICloudBlob log in currentLogs)
{
if (!utcEndTime.HasValue || string.Compare(log.Parent.Prefix, endPrefix) <= 0)
{
if (IsCorrectLogType(log, operations))
{
yield return log;
}
}
else
{
yield break;
}
}
dateCounter = dateCounter.AddMonths(1);
if (dateCounter > DateTimeOffset.UtcNow.AddHours(1))
{
yield break;
}
}
// Continue using a year-based prefix.
while (true)
{
string currentPrefix = logDirectory.Prefix + dateCounter.ToString("yyyy", CultureInfo.InvariantCulture);
IEnumerable<IListBlobItem> currentLogs = logDirectory.Container.ListBlobs(currentPrefix, true, metadataDetails, options, operationContext);
foreach (ICloudBlob log in currentLogs)
{
if (!utcEndTime.HasValue || string.Compare(log.Parent.Prefix, endPrefix) <= 0)
{
if (IsCorrectLogType(log, operations))
{
yield return log;
}
}
else
{
yield break;
}
}
dateCounter = dateCounter.AddYears(1);
if (dateCounter > DateTimeOffset.UtcNow.AddHours(1))
{
yield break;
}
}
}
示例15: TranslatedAdd
private static DateTimeOffset TranslatedAdd(DateTimeOffset date, IntervalUnit unit, int amountToAdd)
{
switch (unit)
{
case IntervalUnit.Day:
return date.AddDays(amountToAdd);
case IntervalUnit.Hour:
return date.AddHours(amountToAdd);
case IntervalUnit.Minute:
return date.AddMinutes(amountToAdd);
case IntervalUnit.Month:
return date.AddMonths(amountToAdd);
case IntervalUnit.Second:
return date.AddSeconds(amountToAdd);
case IntervalUnit.Millisecond:
return date.AddMilliseconds(amountToAdd);
case IntervalUnit.Week:
return date.AddDays(amountToAdd*7);
case IntervalUnit.Year:
return date.AddYears(amountToAdd);
default:
throw new ArgumentException("Unknown IntervalUnit");
}
}