本文整理汇总了C#中DateTime.AddMonths方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.AddMonths方法的具体用法?C# DateTime.AddMonths怎么用?C# DateTime.AddMonths使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime.AddMonths方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InstantToDateTime
public static DateTime InstantToDateTime(double instant, DateTime start, TimeUnits units = TimeUnits.SECONDS)
{
DateTime instantAsDate = start;
switch (units)
{
case TimeUnits.YEARS:
instantAsDate = start.AddYears((int)instant);
break;
case TimeUnits.MONTHS:
instantAsDate = start.AddMonths((int)instant);
break;
case TimeUnits.DAYS:
instantAsDate = start.AddDays(instant);
break;
case TimeUnits.HOURS:
instantAsDate = start.AddHours(instant);
break;
case TimeUnits.MINUTES:
instantAsDate = start.AddMinutes(instant);
break;
case TimeUnits.SECONDS:
instantAsDate = start.AddSeconds(instant);
break;
}
return instantAsDate;
}
示例2: EndOfCurrentmonth
public static DateTime EndOfCurrentmonth(this DateTime value)
{
var retVal = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01);
retVal = retVal.AddMonths(1).AddDays(-1);
return new DateTime(retVal.Year, retVal.Month, retVal.Day);
}
示例3: IsThisMonth
/// <summary>
/// Returns true if the date is this month's date.
/// </summary>
public static bool IsThisMonth(this DateTime time)
{
DateTime now = DateTime.UtcNow;
DateTime monthStart = new DateTime(now.Year, now.Month, 1);
DateTime monthEnd = monthStart.AddMonths(1);
return time > monthStart && time < monthEnd;
}
示例4: CanAddMonthsAcrossDstTransition
public void CanAddMonthsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 2, 9, 0, 0, 0);
var result = dt.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);
}
示例5: DrawMonthCalendarDate
// draws one day in the calendar grid
private void DrawMonthCalendarDate (Graphics dc, Rectangle rectangle, MonthCalendar mc, DateTime date, DateTime month, int row, int col) {
Color date_color = mc.ForeColor;
Rectangle interior = new Rectangle (rectangle.X, rectangle.Y, Math.Max(rectangle.Width - 1, 0), Math.Max(rectangle.Height - 1, 0));
// find out if we are the lead of the first calendar or the trail of the last calendar
if (date.Year != month.Year || date.Month != month.Month) {
DateTime check_date = month.AddMonths (-1);
// check if it's the month before
if (check_date.Year == date.Year && check_date.Month == date.Month && row == 0 && col == 0) {
date_color = mc.TrailingForeColor;
} else {
// check if it's the month after
check_date = month.AddMonths (1);
if (check_date.Year == date.Year && check_date.Month == date.Month && row == mc.CalendarDimensions.Height-1 && col == mc.CalendarDimensions.Width-1) {
date_color = mc.TrailingForeColor;
} else {
return;
}
}
} else {
date_color = mc.ForeColor;
}
const int inflate = -1;
if (date == mc.SelectionStart.Date && date == mc.SelectionEnd.Date) {
// see if the date is in the start of selection
date_color = mc.BackColor;
// draw the left hand of the back ground
Rectangle selection_rect = Rectangle.Inflate (rectangle, inflate, inflate);
dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 0, 360);
} else if (date == mc.SelectionStart.Date) {
// see if the date is in the start of selection
date_color = mc.BackColor;
// draw the left hand of the back ground
Rectangle selection_rect = Rectangle.Inflate (rectangle, inflate, inflate);
dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 90, 180);
// fill the other side as a straight rect
if (date < mc.SelectionEnd.Date)
{
// use rectangle instead of rectangle to go all the way to edge of rect
selection_rect.X = (int) Math.Floor((double)(rectangle.X + rectangle.Width / 2));
selection_rect.Width = Math.Max(rectangle.Right - selection_rect.X, 0);
dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
}
} else if (date == mc.SelectionEnd.Date) {
// see if it is the end of selection
date_color = mc.BackColor;
// draw the left hand of the back ground
Rectangle selection_rect = Rectangle.Inflate (rectangle, inflate, inflate);
dc.FillPie (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect, 270, 180);
// fill the other side as a straight rect
if (date > mc.SelectionStart.Date) {
selection_rect.X = rectangle.X;
selection_rect.Width = rectangle.Width - (rectangle.Width / 2);
dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
}
} else if (date > mc.SelectionStart.Date && date < mc.SelectionEnd.Date) {
// now see if it's in the middle
date_color = mc.BackColor;
// draw the left hand of the back ground
Rectangle selection_rect = Rectangle.Inflate (rectangle, 0, inflate);
dc.FillRectangle (ResPool.GetSolidBrush (mc.TitleBackColor), selection_rect);
}
// establish if it's a bolded font
Font font = mc.IsBoldedDate (date) ? mc.bold_font : mc.Font;
// just draw the date now
dc.DrawString (date.Day.ToString(), font, ResPool.GetSolidBrush (date_color), rectangle, mc.centered_format);
// today circle if needed
if (mc.ShowTodayCircle && date == DateTime.Now.Date) {
DrawTodayCircle (dc, interior);
}
// draw the selection grid
if (mc.is_date_clicked && mc.clicked_date == date) {
Pen pen = ResPool.GetDashPen (Color.Black, DashStyle.Dot);
dc.DrawRectangle (pen, interior);
}
}
示例6: AddMonths
public void AddMonths(DateTime time, int months)
{
Assert.Equal(time.AddMonths(months), new KoreanCalendar().AddMonths(time, months));
}
示例7: DateAndTimeOfDayModelContext
static DateAndTimeOfDayModelContext()
{
DateTime dt = new DateTime(2014, 12, 31);
_dateTimes = Enumerable.Range(1, 5).Select(i =>
new DateAndTimeOfDayModel
{
Id = i,
Birthday = dt.AddYears(i),
PublishDay = i % 2 == 0 ? (DateTime?)null : dt.AddMonths(i),
CreatedTime = new TimeSpan(0, i, 4, 5, 198),
EdmTime = i % 3 == 0 ? (TimeSpan?)null : new TimeSpan(0, 8, i, 5, 198),
ResumeTime = new TimeSpan(0, i, 4, 5, 198)
}).ToList();
}
示例8: AddMonths
public void AddMonths(DateTime time, int months)
{
Assert.Equal(time.AddMonths(months), new ThaiBuddhistCalendar().AddMonths(time, months));
}
示例9: DisplayDateRangeEnd
public void DisplayDateRangeEnd()
{
Calendar calendar = new Calendar();
DateTime value = new DateTime(2000, 1, 30);
calendar.DisplayDate = value;
calendar.DisplayDateEnd = value;
calendar.DisplayDateStart = value;
Assert.IsTrue(CompareDates(calendar.DisplayDateStart.Value, value));
Assert.IsTrue(CompareDates(calendar.DisplayDateEnd.Value, value));
value = value.AddMonths(2);
calendar.DisplayDateStart = value;
Assert.IsTrue(CompareDates(calendar.DisplayDateStart.Value, value));
Assert.IsTrue(CompareDates(calendar.DisplayDateEnd.Value, value));
Assert.IsTrue(CompareDates(calendar.DisplayDate, value));
}
示例10: DateTimeModelContext
static DateTimeModelContext()
{
DateTime dt = new DateTime(2014, 12, 31, 20, 12, 30, DateTimeKind.Utc);
_dateTimes = Enumerable.Range(1, 5).Select(i =>
new DateTimeModel
{
Id = i,
BirthdayA = dt.AddYears(i),
BirthdayB = dt.AddMonths(i),
BirthdayC = new List<DateTime> { dt.AddYears(i), dt.AddMonths(i), dt.AddDays(i) },
BirthdayD = new List<DateTime?>
{
dt.AddYears(2 * i), null, dt.AddMonths(2 * i)
}
}).ToList();
}