/// <summary> /// 返回两个日期之间的时间间隔(y:年份间隔、M:月份间隔、d:天数间隔、h:小时间隔、m:分钟间隔、s:秒钟间隔、ms:微秒间隔) /// </summary> /// <param name="Date1">开始日期</param> /// <param name="Date2">结束日期</param> /// <param name="Interval">间隔标志</param> /// <returns>返回间隔标志指定的时间间隔</returns> public static int DateDiff(System.DateTime Date1, System.DateTime Date2, string Interval) { double dblYearLen = 365;//年的长度,365天 double dblMonthLen = (365 / 12);//每个月平均的天数 System.TimeSpan objT; objT = Date2.Subtract(Date1); switch (Interval) { case "y"://返回日期的年份间隔 return System.Convert.ToInt32(objT.Days / dblYearLen); case "M"://返回日期的月份间隔 return System.Convert.ToInt32(objT.Days / dblMonthLen); case "d"://返回日期的天数间隔 return objT.Days; case "h"://返回日期的小时间隔 return objT.Hours; case "m"://返回日期的分钟间隔 return objT.Minutes; case "s"://返回日期的秒钟间隔 return objT.Seconds; case "ms"://返回时间的微秒间隔 return objT.Milliseconds; default: break; } return 0; }
/// <summary> /// This implementation was found on http://stackoverflow.com/questions/1285191/get-week-of-date-from-linq-query /// </summary> /// <param name="fromDate"></param> /// <returns></returns> private int WeekNumber(System.DateTime fromDate) { // Get jan 1st of the year var startOfYear = fromDate.AddDays(-fromDate.Day + 1).AddMonths(-fromDate.Month + 1); // Get dec 31st of the year var endOfYear = startOfYear.AddYears(1).AddDays(-1); // ISO 8601 weeks start with Monday // The first week of a year includes the first Thursday // DayOfWeek returns 0 for sunday up to 6 for saterday int[] iso8601Correction = { 6, 7, 8, 9, 10, 4, 5 }; int nds = fromDate.Subtract(startOfYear).Days + iso8601Correction[(int)startOfYear.DayOfWeek]; int wk = nds / 7; switch (wk) { case 0: // Return weeknumber of dec 31st of the previous year return WeekNumber(startOfYear.AddDays(-1)); case 53: // If dec 31st falls before thursday it is week 01 of next year if (endOfYear.DayOfWeek < DayOfWeek.Thursday) return 1; return wk; default: return wk; } }
float getDayHourMinFractionOfWeek(System.DateTime dt) { System.DateTime sunday = getSundayH00M00S00 (dt); int daysdiff = dt.Subtract (sunday).Days; int hourMin_min = dt.Hour * 60 + dt.Minute; float hourMinFraction = (float)hourMin_min / (24f * 60f); // 24 hours x 60 minutes float ddhhmmFraction = (float)daysdiff + hourMinFraction; Debug.Log ("target: " + dt.ToString ()); Debug.Log ("sunday: " + sunday.ToString ()); Debug.Log ("days: " + daysdiff); Debug.Log ("ddhhmmFraction: " + ddhhmmFraction); return ddhhmmFraction / 7.0f; // 7 days a week }
public static System.DateTime GetFirstDayOfWeekDate(System.DateTime myDate) { switch (myDate.DayOfWeek) { case DayOfWeek.Sunday: myDate = myDate.Subtract(new TimeSpan(6, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; case DayOfWeek.Monday: myDate = myDate.Subtract(new TimeSpan(0, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; case DayOfWeek.Tuesday: myDate = myDate.Subtract(new TimeSpan(1, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; case DayOfWeek.Wednesday: myDate = myDate.Subtract(new TimeSpan(2, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; case DayOfWeek.Thursday: myDate = myDate.Subtract(new TimeSpan(3, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; case DayOfWeek.Friday: myDate = myDate.Subtract(new TimeSpan(4, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; case DayOfWeek.Saturday: myDate = myDate.Subtract(new TimeSpan(5, 0, 0, 0)); break; // TODO: might not be correct. Was : Exit Select break; } return myDate; }
/// <summary> Converts a Date to a string suitable for indexing.</summary> /// <throws> SystemException if the date specified in the </throws> /// <summary> method argument is before 1970 /// </summary> public static System.String DateToString(System.DateTime date) { TimeSpan ts = date.Subtract(new DateTime(1970, 1, 1)); ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(date)); return TimeToString(ts.Ticks / TimeSpan.TicksPerMillisecond); }
public static long GetJavascriptTimestamp(System.DateTime input) { return (long)input.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds; }
/// <summary>Years Between 2 dates this will output a decimal to 0dp /// <para>StartDate = Start Date for the calculation</para> /// <para>EndDate = End Date for the calculation</para> /// <para>inclusive = Adds 1 day on the end</para> /// <para>daysperyear = set days per year</para> /// </summary> public double YearsBetween(System.DateTime StartDate, System.DateTime EndDate, bool inclusive, double daysperyear) { System.TimeSpan d; double days; d = EndDate.Subtract(StartDate); days = d.Days; double Years = Math.Floor(days / daysperyear); return Years; }
/// <summary> /// Subtracts a TimeSpan from a DateTime, yielding a new DateTime. /// </summary> /// <param name="dateTime">Starting DateTime.</param> /// <param name="timeSpan">Amount of time to subtract.</param> /// <returns name="dateTime">DateTime</returns> public static System.DateTime SubtractTimeSpan(System.DateTime dateTime, System.TimeSpan timeSpan) { return dateTime.Subtract(timeSpan); }
public long GetJavascriptTimestamp(System.DateTime input) { System.TimeSpan span = new System.TimeSpan(System.DateTime.Parse("1/1/1970").Ticks); System.DateTime time = input.Subtract(span); return (long)(time.Ticks / 10000); }
/// <summary> /// Yields a new TimeSpan calculated from the time difference between two DateTimes. /// </summary> /// <param name="date1">Starting DateTime.</param> /// <param name="date2">Ending DateTime.</param> /// <returns name="timeSpan">TimeSpan</returns> public static System.TimeSpan ByDateDifference(System.DateTime date1, System.DateTime date2) { return date1.Subtract(date2); }
/// <summary> /// Subtracts two TimeSpans. /// </summary> /// <param name="timeSpan1">A TimeSpan.</param> /// <param name="timeSpan2">A TimeSpan.</param> /// <returns name="timeSpan">TimeSpan</returns> public static System.TimeSpan Subtract(System.TimeSpan timeSpan1, System.TimeSpan timeSpan2) { return timeSpan1.Subtract(timeSpan2); }
/// <summary>Days Between 2 dates this will output a decimal to 0dp /// <para>StartDate = Start Date for the calculation</para> /// <para>EndDate = End Date for the calculation</para> /// <para>inclusive = Adds 1 day on the end</para> /// <para>daysperyear = set days per year</para> /// </summary> public double DaysBetween(System.DateTime StartDate, System.DateTime EndDate, bool inclusive, double daysperyear) { System.TimeSpan d; double days; d = EndDate.Subtract(StartDate); days = d.Days; if (inclusive == true) { days = days + 1; } return days; }
private static int GetUnixTime(System.DateTime UtcDateTime) { System.DateTime Epoch = new System.DateTime(1970, 1, 1); return (int)UtcDateTime.Subtract(Epoch).TotalSeconds; }
private Rectangle DateTimePlacement(System.DateTime CurrentMarker) { long range = 0; if (mMarkers == null) { return Rectangle.Empty; } range = mMarkers[mMarkers.GetUpperBound(0)].Subtract(mMarkers[mMarkers.GetLowerBound(0)]).Days; if (range == 0 || range == Int64.MaxValue) { return Rectangle.Empty; } Rectangle r = Rectangle.Inflate(mMarker, 0, 0); r.Offset(System.Convert.ToInt32(mPadding + (this.Width - r.Width - mPadding * 2) * CurrentMarker.Subtract(mMarkers[0]).Days / range), mPadding); return r; }
internal void SetToDate(System.DateTime date) { this._type = StorageType.Date; this._value._int32 = date.Subtract(System.DateTime.MinValue).Days; this._isNull = false; }
/// <summary>Years Days Between 2 dates this will output a decimal to 3dp with the dp representing days. /// <para>StartDate = Start Date for the calculation</para> /// <para>EndDate = End Date for the calculation</para> /// <para>inclusive = Adds 1 day on the end</para> /// <para>daysperyear = set days per year</para> /// </summary> public double YearsDaysBetween(System.DateTime StartDate, System.DateTime EndDate, bool inclusive, double daysperyear) { System.TimeSpan d; double days; d = EndDate.Subtract(StartDate); days = d.Days; if(inclusive == true) { days = days + 1; } double Years = Math.Floor(days / daysperyear); days = (days / daysperyear) - Years; days = Math.Floor(days * daysperyear); days = Math.Round((days / 1000),3,MidpointRounding.AwayFromZero); days = Years + days; return days; }
/// <summary> /// Given a Start and End Time calculates and produces a Process Run Time string in hours, minutes /// and seconds (as necessary). /// </summary> /// <param name="startTime"> /// The time that the process run began. /// </param> /// <param name="endTime"> /// The time that the process run ended. /// </param> /// <returns> /// A string containing the process run time in hours, minutes and seconds (as necessary - for example: /// 3 hours 15 minutes and 30 seconds, 10 minutes and 25 seconds) . /// </returns> public string CalculateProcessRunTime(System.DateTime StartTime, System.DateTime EndTime) { try { // Determine the process run time. System.TimeSpan runTime = EndTime.Subtract(StartTime); // Break the Process Run Time into its component parts (hours, minutes, seconds) double seconds = (int)runTime.TotalSeconds; double minutes = (int)runTime.TotalMinutes; double hours = (int)runTime.TotalHours; double days = (int)runTime.TotalDays; seconds = (seconds - (minutes * 60)); minutes = (minutes - (hours * 60)); hours = (hours - (days * 24)); // Start to build the Time String. string timeString = null; if (seconds == 1) { timeString = seconds.ToString() + " second"; } else { timeString = seconds.ToString() + " seconds"; } // If the Days, Hours or Minutes value is non-zero, build a Time String including a // number of minutes entry. if ((days > 0) || (hours > 0) || (minutes > 0)) { // Build a Time String including a number of minutes entry. if (minutes == 1) { timeString = minutes.ToString() + " minute and " + timeString; } else { timeString = minutes.ToString() + " minutes and " + timeString; } } // If the Days or Hours value is non-zero, build a Time String including a number // of hours entry. if ((days > 0) || (hours > 0)) { if (hours == 1) { // Add the number of hours the process ran to the Time String. timeString = hours.ToString() + " hour " + timeString; } else { // Add the number of hours the process ran to the Time String. timeString = hours.ToString() + " hours " + timeString; } } // If the Days value is non-zero, build a Time String including a number // of days entry. if (days > 0) { if (days == 1) { // Add the number of hours the process ran to the Time String. timeString = days.ToString() + " day " + timeString; } else { // Add the number of hours the process ran to the Time String. timeString = days.ToString() + " days " + timeString; } } // Return the process run time string. return timeString; } catch { // Return a NULL string to indicate taht this process failed. return null; } }
private void PushSprintDates(int sprintNumber, System.DateTime newStartDate, System.DateTime newEndDate, ICollection<Sprint> sprints) { // make sure sprints are ordered by number Sprint[] orderedSprint = sprints.OrderBy(s => s.SprintNumber).ToArray(); // finds the sprint Sprint sprint = orderedSprint.SingleOrDefault(s => s.SprintNumber == sprintNumber); if (sprint == null) return; if (sprintNumber >= 2) { Sprint previous = orderedSprint[sprintNumber - 2]; if (newStartDate <= previous.StartDate.AddDays(1)) throw new WebFaultException<String>("BRE_SPRINT_START_DATE_NOT_ALLOWED", System.Net.HttpStatusCode.BadRequest); } if (newEndDate < sprint.StartDate) throw new WebFaultException<String>("BRE_SPRINT_END_DATE_NOT_ALLOWED", System.Net.HttpStatusCode.BadRequest); // get the number of days to push the sprints int pushDays = 0; int startDatePushDays = (sprintNumber == 1) ? newStartDate.Subtract(sprint.StartDate).Days : 0; // if is the first sprint and the start date was changed, it override the end date change if (startDatePushDays != 0) { newEndDate = newEndDate.AddDays(startDatePushDays); pushDays = startDatePushDays; } else { pushDays = newEndDate.Subtract(sprint.EndDate).Days; } if (newStartDate > newEndDate) throw new WebFaultException<String>("BRE_SPRINT_START_DATE_NOT_ALLOWED", System.Net.HttpStatusCode.BadRequest); // update the sprint start date sprint.StartDate = newStartDate; sprint.EndDate = newEndDate; // pushs the following sprints, if this one is not the last if (sprintNumber <= orderedSprint.Length && pushDays != 0) { for (int i = sprintNumber; i < orderedSprint.Length; i++) { int days = calendar.CalcWorkDayCount(orderedSprint[i].StartDate, orderedSprint[i].EndDate); orderedSprint[i].StartDate = calendar.AddWorkDays(orderedSprint[i - 1].EndDate, 1); orderedSprint[i].EndDate = calendar.AddWorkDays(orderedSprint[i].StartDate, days); } } // moves the previus sprint end date one day before this one start date if (sprintNumber > 1) orderedSprint[sprintNumber - 2].EndDate = calendar.SubWorkDays(newStartDate, 1); }
public static int getDaysFrom(System.DateTime fromDt, System.DateTime baseDt) { return fromDt.Subtract (baseDt).Days; }
public static double SecondsDiff(System.DateTime date1, System.DateTime date2) { return ((TimeSpan)(date2.Subtract(date1))).TotalSeconds; }
internal void SetToDateTime2(System.DateTime dateTime, byte scale) { this._type = StorageType.DateTime2; this._value._dateTime2Info.timeInfo.ticks = dateTime.TimeOfDay.Ticks; this._value._dateTime2Info.timeInfo.scale = scale; this._value._dateTime2Info.date = dateTime.Subtract(System.DateTime.MinValue).Days; this._isNull = false; }
public static System.TimeSpan GetTimeSpan(this System.DateTime startTime, System.DateTime endTime) { return endTime.Subtract(startTime); }