本文整理汇总了C#中DateTime.AddDays方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.AddDays方法的具体用法?C# DateTime.AddDays怎么用?C# DateTime.AddDays使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime.AddDays方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FirstWeekDayOfMonth
/// <summary>
/// Returns the first weekday (Financial day) of the month
/// </summary>
/// <param name="obj">DateTime Base, from where the calculation will be preformed.</param>
/// <returns>Returns DateTime object that represents the first weekday (Financial day) of the month</returns>
public static DateTime FirstWeekDayOfMonth(this DateTime obj)
{
var firstDay = new DateTime(obj.Year, obj.Month, 1);
for (int i = 0; i < 7; i++)
{
if (firstDay.AddDays(i).DayOfWeek != DayOfWeek.Saturday && firstDay.AddDays(i).DayOfWeek != DayOfWeek.Sunday)
return firstDay.AddDays(i);
}
return firstDay;
}
示例2: LastWeekDayOfMonth
/// <summary>
/// Returns the last weekday (Financial day) of the month
/// </summary>
/// <param name="obj">DateTime Base, from where the calculation will be preformed.</param>
/// <returns>Returns DateTime object that represents the last weekday (Financial day) of the month</returns>
public static DateTime LastWeekDayOfMonth(this DateTime obj)
{
var lastDay = new DateTime(obj.Year, obj.Month, DateTime.DaysInMonth(obj.Year, obj.Month));
for (int i = 0; i < 7; i++)
{
if (lastDay.AddDays(i * -1).DayOfWeek != DayOfWeek.Saturday && lastDay.AddDays(i * -1).DayOfWeek != DayOfWeek.Sunday)
return lastDay.AddDays(i * -1);
}
return lastDay;
}
示例3: 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;
}
示例4: FirstDayOfWeekInMonth
/// <summary>
/// Returns the first day of week with in the month.
/// </summary>
/// <param name="obj">DateTime Base, from where the calculation will be preformed.</param>
/// <param name="dow">What day of week to find the first one of in the month.</param>
/// <returns>Returns DateTime object that represents the first day of week with in the month.</returns>
public static DateTime FirstDayOfWeekInMonth(this DateTime obj, DayOfWeek dow)
{
var firstDay = new DateTime(obj.Year, obj.Month, 1);
int diff = firstDay.DayOfWeek - dow;
if (diff > 0) diff -= 7;
return firstDay.AddDays(diff * -1);
}
示例5: GetNonWorkingDays
public static IList<DateTime> GetNonWorkingDays(int year)
{
IList<DateTime> days = new List<DateTime>();
days.Add(new DateTime(year, 01, 01));
days.Add(new DateTime(year, 01, 06));
days.Add(new DateTime(year, 04, 25));
days.Add(new DateTime(year, 05, 01));
days.Add(new DateTime(year, 06, 02));
days.Add(new DateTime(year, 06, 24));
days.Add(new DateTime(year, 08, 15));
days.Add(new DateTime(year, 10, 01));
days.Add(new DateTime(year, 12, 08));
days.Add(new DateTime(year, 12, 25));
days.Add(new DateTime(year, 12, 26));
days.Add(NonWorkingDays.EasterDay(year));
days.Add(NonWorkingDays.EasterDay(year).AddDays(1));
DateTime dummy = new DateTime(year, 1, 1);
while (dummy.Year == year)
{
if (!days.Contains(dummy) && dummy.DayOfWeek == DayOfWeek.Saturday || dummy.DayOfWeek == DayOfWeek.Sunday)
{
days.Add(dummy);
}
dummy = dummy.AddDays(1);
}
return days;//a
}
示例6: FirstMonday
private int FirstMonday(int Year, int Month)
{
DateTime FirstMonday = new DateTime(Year, Month, 1);
while (!(FirstMonday.DayOfWeek == DayOfWeek.Monday))
{
FirstMonday = FirstMonday.AddDays(1);
}
return FirstMonday.Day;
}
示例7: LastDayOfWeekInMonth
/// <summary>
/// Returns the last day of week with in the month.
/// </summary>
/// <param name="obj">DateTime Base, from where the calculation will be preformed.</param>
/// <param name="dow">What day of week to find the last one of in the month.</param>
/// <returns>Returns DateTime object that represents the last day of week with in the month.</returns>
public static DateTime LastDayOfWeekInMonth(this DateTime obj, DayOfWeek dow)
{
var lastDay = new DateTime(obj.Year, obj.Month, DateTime.DaysInMonth(obj.Year, obj.Month));
DayOfWeek lastDow = lastDay.DayOfWeek;
int diff = dow - lastDow;
if (diff > 0) diff -= 7;
return lastDay.AddDays(diff);
}
示例8: CanAddDaysAcrossDstTransition_LandInOverlap
public void CanAddDaysAcrossDstTransition_LandInOverlap()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 10, 31, 1, 30, 0);
var result = dt.AddDays(1, tz);
var expected = new DateTimeOffset(2015, 11, 1, 1, 30, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}
示例9: CanAddDaysAcrossDstTransition_StartWithMismatchedKind
public void CanAddDaysAcrossDstTransition_StartWithMismatchedKind()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 8, 0, 0, DateTimeKind.Utc);
var result = dt.AddDays(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);
}
示例10: LastSunday
private int LastSunday(int Year, int Month)
{
int LastDay = DateTime.DaysInMonth(Year, Month);
DateTime LastSunday = new DateTime(Year, Month, LastDay);
while (!(LastSunday.DayOfWeek == DayOfWeek.Sunday))
{
LastSunday = LastSunday.AddDays(-1);
}
return LastSunday.Day;
}
示例11: Default
public static DateTimeOffset Default(DateTime dt, TimeZoneInfo timeZone)
{
if (dt.Kind != DateTimeKind.Unspecified)
{
var dto = new DateTimeOffset(dt);
return TimeZoneInfo.ConvertTime(dto, timeZone);
}
if (timeZone.IsAmbiguousTime(dt))
{
var earlierOffset = timeZone.GetUtcOffset(dt.AddDays(-1));
return new DateTimeOffset(dt, earlierOffset);
}
if (timeZone.IsInvalidTime(dt))
{
var earlierOffset = timeZone.GetUtcOffset(dt.AddDays(-1));
var laterOffset = timeZone.GetUtcOffset(dt.AddDays(1));
var transitionGap = laterOffset - earlierOffset;
return new DateTimeOffset(dt.Add(transitionGap), laterOffset);
}
return new DateTimeOffset(dt, timeZone.GetUtcOffset(dt));
}
示例12: GetBuildDateTime
/// <summary>
/// 在C#中指定修订号为*的时候,修订号则是一个时间戳,我们可以从其得到编译日期
/// 生成和修订版本号必须是自动生成的
/// AssemblyInfo里的写法应该为1.0之后为.*
/// [assembly: AssemblyVersion("1.0.*")]
/// </summary>
/// <returns></returns>
public DateTime GetBuildDateTime()
{
/*
* version = 1.0.3420.56234
* 这里主版本号是1,次版本号是0,而生成和修订版本号是自动生成的。
* 使用*时,生成号是从2000年1月1日开始的天数,而修订号是从凌晨开始的秒数除以2。
*/
string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
int days = 0;
int seconds = 0;
string[] v = version.Split('.');
if (v.Length == 4)
{
days = Convert.ToInt32(v[2]);
seconds = Convert.ToInt32(v[3]);
}
DateTime dt = new DateTime(2000, 1, 1);
dt = dt.AddDays(days);
dt = dt.AddSeconds(seconds * 2);
return dt;
}
示例13: GetPreviousSnap
public static String GetPreviousSnap(String destRootDir, DateTime baseDate, Int32 prevLimit)
{
// -1���Ƃ�
if (prevLimit < 1) throw new ArgumentException("�O����s���̐������s���ł�");
for (Int32 i = 0; i < prevLimit; i++)
{
DateTime prevDate = baseDate.AddDays(~i);
String prevDir = Path.Combine(destRootDir, prevDate.ToString("yyyy") + Path.DirectorySeparatorChar + prevDate.ToString("MM") + Path.DirectorySeparatorChar + prevDate.ToString("dd"));
//String prevDir = Path.Combine(destRootDir, prevDate.ToString("yyyyMMdd"));
if (Directory.Exists(prevDir)) return prevDir;
}
return "";
}
示例14: GetPreviousWorkingDay
/// <summary>
/// Get the previous working based on the project calendar.
/// </summary>
/// <param name="day">The day to get the previous day for.</param>
/// <returns>The previous day.</returns>
public DateTime GetPreviousWorkingDay(DateTime day)
{
DateTime previousDay = day.AddDays(-1);
while (!IsWorkingDay(previousDay))
previousDay = previousDay.AddDays(-1);
return previousDay;
}
示例15: DayButton_AutomationPeer
public void DayButton_AutomationPeer()
{
Calendar calendar = new Calendar();
Assert.IsNotNull(calendar);
_isLoaded = false;
DateTime date = new DateTime(2000, 2, 2);
calendar.DisplayDate = date;
calendar.SelectedDate = date;
CalendarAutomationPeer calendarAutomationPeer = (CalendarAutomationPeer)CalendarAutomationPeer.CreatePeerForElement(calendar);
Assert.IsNotNull(calendarAutomationPeer);
TestPeer testPeer = new TestPeer(calendar);
calendar.Loaded += new RoutedEventHandler(calendar_Loaded);
TestPanel.Children.Add(calendar);
EnqueueConditional(IsLoaded);
EnqueueCallback(delegate
{
CalendarDayButton dayButton = calendar.FindDayButtonFromDay(date);
Assert.IsNotNull(dayButton);
AutomationPeer peer = CalendarAutomationPeer.CreatePeerForElement(dayButton);
Assert.IsNotNull(peer);
Assert.AreEqual(peer.GetAutomationControlType(), AutomationControlType.Button, "Incorrect Control type for Daybutton");
Assert.AreEqual(peer.GetClassName(), dayButton.GetType().Name, "Incorrect ClassName value for DayButton");
Assert.AreEqual(peer.GetName(), dayButton.Content.ToString(), "Incorrect Name value for DayButtonPeer");
Assert.IsTrue(peer.IsContentElement(), "Incorrect IsContentElement value");
Assert.IsTrue(peer.IsControlElement(), "Incorrect IsControlElement value");
Assert.IsFalse(peer.IsKeyboardFocusable(), "Incorrect IsKeyBoardFocusable value");
#region DayButtonAutomationPeer ISelectionItemProvider tests:
ISelectionItemProvider selectionItem = (ISelectionItemProvider)peer.GetPattern(PatternInterface.SelectionItem);
Assert.IsNotNull(selectionItem);
Assert.IsTrue(selectionItem.IsSelected);
Assert.AreEqual(calendarAutomationPeer, testPeer.GetPeerFromProvider(selectionItem.SelectionContainer));
selectionItem.RemoveFromSelection();
Assert.IsFalse(selectionItem.IsSelected);
selectionItem.AddToSelection();
Assert.IsTrue(selectionItem.IsSelected);
//check selection in SingleDate mode
CalendarDayButton dayButton2 = calendar.FindDayButtonFromDay(date.AddDays(1));
Assert.IsNotNull(dayButton2);
AutomationPeer peer2 = CalendarAutomationPeer.CreatePeerForElement(dayButton2);
Assert.IsNotNull(peer2);
ISelectionItemProvider selectionItem2 = (ISelectionItemProvider)peer2.GetPattern(PatternInterface.SelectionItem);
Assert.IsNotNull(selectionItem2);
Assert.IsFalse(selectionItem2.IsSelected);
selectionItem2.AddToSelection();
Assert.IsTrue(selectionItem2.IsSelected);
Assert.IsFalse(selectionItem.IsSelected);
//check blackout day
selectionItem.RemoveFromSelection();
calendar.BlackoutDates.Add(new CalendarDateRange(date));
selectionItem.AddToSelection();
Assert.IsFalse(selectionItem.IsSelected);
//check selection in None mode
calendar.SelectionMode = CalendarSelectionMode.None;
Assert.IsFalse(selectionItem2.IsSelected);
selectionItem2.AddToSelection();
Assert.IsFalse(selectionItem2.IsSelected);
//check selection in MultiRange mode
calendar.BlackoutDates.Clear();
calendar.SelectionMode = CalendarSelectionMode.MultipleRange;
Assert.IsFalse(selectionItem.IsSelected);
Assert.IsFalse(selectionItem2.IsSelected);
selectionItem.AddToSelection();
selectionItem2.AddToSelection();
Assert.IsTrue(selectionItem.IsSelected);
Assert.IsTrue(selectionItem2.IsSelected);
selectionItem2.RemoveFromSelection();
Assert.IsTrue(selectionItem.IsSelected);
Assert.IsFalse(selectionItem2.IsSelected);
#endregion
#region DayButtonAutomationPeer IInvoke tests:
//check selection and trailing day functionality
CalendarDayButton dayButton4 = calendar.FindDayButtonFromDay(new DateTime(2000,1,31));
Assert.IsNotNull(dayButton4);
AutomationPeer peer4 = CalendarAutomationPeer.CreatePeerForElement(dayButton4);
Assert.IsNotNull(peer4);
IInvokeProvider invokeItem = (IInvokeProvider)peer4.GetPattern(PatternInterface.Invoke);
Assert.IsNotNull(invokeItem);
invokeItem.Invoke();
dayButton4 = calendar.FindDayButtonFromDay(new DateTime(2000, 1, 31));
Assert.IsTrue(dayButton4.IsSelected);
Assert.AreEqual(calendar.DisplayDate.Month, 1);
#endregion
#region DayButtonAutomationPeer ITableItemProvider tests:
ITableItemProvider tableItem = (ITableItemProvider)peer.GetPattern(PatternInterface.TableItem);
//.........这里部分代码省略.........