/// <summary> /// 计算指定的两个日期之间的较为确切的浮点数时间差 /// </summary> /// <param name="eInterval">时间差类型</param> /// <param name="dtBeginTime">起始时间</param> /// <param name="dtEndTime">截止时间</param> /// <returns>时间差</returns> public static float DateDiffExact(DateInterval eInterval, DateTime dtBeginTime, DateTime dtEndTime) { switch (eInterval) { case DateInterval.Hour: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((float)span.TotalHours); } case DateInterval.Minute: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((float)span.TotalMinutes); } case DateInterval.Second: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((float)span.TotalSeconds); } case DateInterval.Year: { //仅精确到小数天 System.Globalization.Calendar currentCalendar = null; currentCalendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; int years = currentCalendar.GetYear(dtEndTime) - currentCalendar.GetYear(dtBeginTime); TimeSpan span = dtEndTime.Subtract(dtBeginTime.AddYears(years)); return(((float)years) + ((float)span.TotalDays / 365f)); } case DateInterval.Month: { //仅精确到小数天 System.Globalization.Calendar currentCalendar = null; currentCalendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; int months = (currentCalendar.GetYear(dtEndTime) - currentCalendar.GetYear(dtBeginTime)) * 12 + (currentCalendar.GetMonth(dtEndTime) - currentCalendar.GetMonth(dtBeginTime)); TimeSpan span = dtEndTime.Subtract(dtBeginTime.AddMonths(months)); return(((float)months) + ((float)span.TotalDays / 30f)); } default: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((float)span.TotalDays); } } }
public void Read(DateTime date) { var year = _calendar.GetYear(date); var month = _calendar.GetMonth(date); var day = _calendar.GetDayOfMonth(date); Read(year, month, day, date.Hour, date.Minute, date.Second, date.Millisecond); }
/// <summary> /// 计算指定的两个日期之间的时间差 /// </summary> /// <param name="eInterval">时间差类型</param> /// <param name="dtBeginTime">起始时间</param> /// <param name="dtEndTime">截止时间</param> /// <returns>时间差</returns> public static long DateDiff(DateInterval eInterval, DateTime dtBeginTime, DateTime dtEndTime) { switch (eInterval) { case DateInterval.Hour: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((long)Math.Round(GlobalMethods.Convert.Fix(span.TotalHours))); } case DateInterval.Minute: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((long)Math.Round(GlobalMethods.Convert.Fix(span.TotalMinutes))); } case DateInterval.Second: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((long)Math.Round(GlobalMethods.Convert.Fix(span.TotalSeconds))); } case DateInterval.Year: { System.Globalization.Calendar currentCalendar = null; currentCalendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; return((long)(currentCalendar.GetYear(dtEndTime) - currentCalendar.GetYear(dtBeginTime))); } case DateInterval.Month: { System.Globalization.Calendar currentCalendar = null; currentCalendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; return((long)((((currentCalendar.GetYear(dtEndTime) - currentCalendar.GetYear(dtBeginTime)) * 12) + currentCalendar.GetMonth(dtEndTime)) - currentCalendar.GetMonth(dtBeginTime))); } default: { TimeSpan span = dtEndTime.Subtract(dtBeginTime); return((long)Math.Round(GlobalMethods.Convert.Fix(span.TotalDays))); } } }
public override void GenerateFileName() { System.Globalization.CultureInfo cult_info = System.Globalization.CultureInfo.CreateSpecificCulture("no"); System.Globalization.Calendar cal = cult_info.Calendar; int weekCount = cal.GetWeekOfYear(DateTime.Now, cult_info.DateTimeFormat.CalendarWeekRule, cult_info.DateTimeFormat.FirstDayOfWeek); int monthCount = cal.GetMonth(DateTime.Now); //filename = "log." + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + ".txt"; filename = "log." + weekCount.ToString() + DateTime.Now.Year.ToString() + ".txt"; }
private void InitializeMe() { System.Globalization.Calendar c1 = _GetCalendar(_Calendar); if (_DateTime != null && _DateTime.Value.Year > 690) { Year = c1.GetYear(_DateTime.Value); Month = c1.GetMonth(_DateTime.Value); Day = c1.GetDayOfMonth(_DateTime.Value); DayOfWeek = c1.GetDayOfWeek(_DateTime.Value); } else { _DateTime = null; } }
// TODO: Need more work to accept (") and (') and (%) and (\) characters. public static string ToCurrentCultureString(DateTime dt, string format, System.Globalization.DateTimeFormatInfo formatProvider) { // Some formats do not need to custom implementation like these string[] autoReplaces = new string[] { "fffffff", "ffffff", "fffff", "ffff", "fff", "ff", "f", "FFFFFFF", "FFFFFF", "FFFFF", "FFFF", "FFF", "FF", "F", "gg", "g", "hh", "HH", "mm", "ss", "tt", "t" }; System.Globalization.Calendar cal = GetCurrentCalendar(); int year = cal.GetYear(dt); int month = cal.GetMonth(dt); int day = cal.GetDayOfMonth(dt); DayOfWeek dayOfWeek = cal.GetDayOfWeek(dt); foreach (string autoReplace in autoReplaces) { format = format.Replace(autoReplace, dt.ToString(autoReplace, formatProvider)); } format = format.Replace("dddd", formatProvider.GetDayName(dayOfWeek)); format = format.Replace("ddd", formatProvider.GetAbbreviatedDayName(dayOfWeek)); format = format.Replace("dd", ((int)dayOfWeek).ToString("00")); format = format.Replace("dd", dayOfWeek.ToString()); format = format.Replace("MMMM", formatProvider.GetMonthName(month)); format = format.Replace("MMM", formatProvider.GetAbbreviatedMonthName(month)); format = format.Replace("MM", month.ToString("00")); format = format.Replace("M", month.ToString()); format = format.Replace("yyyy", year.ToString("0000")); format = format.Replace("yyy", year.ToString("000")); format = format.Replace("yy", (year % 100).ToString("00")); format = format.Replace("y", (year % 100).ToString()); return(format); }
/// <summary> /// Checks whether RadCalendarDay object is associated with a DateTime that represents a recurring event. /// </summary> /// <param name="compareTime">the DateTime to compare.</param> /// <param name="processCalendar">the System.Globalization.Calendar object used to check whether the DateTime /// represents a recurring event.</param> /// <returns></returns> internal protected virtual RecurringEvents IsRecurring(DateTime compareTime, System.Globalization.Calendar processCalendar) { if (Recurring != RecurringEvents.None) { switch (Recurring) { case RecurringEvents.DayInMonth: { int firstCompare = processCalendar.GetDayOfMonth(compareTime); int secondCompare = processCalendar.GetDayOfMonth(this.Date); if (firstCompare.Equals(secondCompare)) { return(Recurring); } } break; case RecurringEvents.Today: if (compareTime.Equals(DateTime.Today)) { return(Recurring); } break; case RecurringEvents.DayAndMonth: { int FirstCompare = processCalendar.GetDayOfMonth(compareTime); int SecondCompare = processCalendar.GetDayOfMonth(this.Date); int FirstMonthCompare = processCalendar.GetMonth(compareTime); int SecondMonthCompare = processCalendar.GetMonth(this.Date); if (FirstCompare.Equals(SecondCompare) && FirstMonthCompare.Equals(SecondMonthCompare)) { return(Recurring); } } break; case RecurringEvents.WeekAndMonth: { DayOfWeek FirstCompare = processCalendar.GetDayOfWeek(compareTime); DayOfWeek SecondCompare = processCalendar.GetDayOfWeek(this.Date); int FirstMonthCompare = processCalendar.GetMonth(compareTime); int SecondMonthCompare = processCalendar.GetMonth(this.Date); if (FirstCompare.Equals(SecondCompare) && FirstMonthCompare.Equals(SecondMonthCompare)) { return(Recurring); } break; } case RecurringEvents.Week: { DayOfWeek FirstCompare = processCalendar.GetDayOfWeek(compareTime); DayOfWeek SecondCompare = processCalendar.GetDayOfWeek(this.Date); if (FirstCompare.Equals(SecondCompare)) { return(Recurring); } break; } default: break; } } return(RecurringEvents.None); }
public static Int64 DateDiff(this DateTime StartDate, String DatePart, DateTime EndDate) { Int64 DateDiffVal = 0; System.Globalization.Calendar cal = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; TimeSpan ts = new TimeSpan(EndDate.Ticks - StartDate.Ticks); switch (DatePart.ToLower().Trim()) { case "year": case "yy": case "yyyy": DateDiffVal = (Int64)(cal.GetYear(EndDate) - cal.GetYear(StartDate)); break; case "quarter": case "qq": case "q": DateDiffVal = (Int64)((((cal.GetYear(EndDate) - cal.GetYear(StartDate)) * 4) + ((cal.GetMonth(EndDate) - 1) / 3)) - ((cal.GetMonth(StartDate) - 1) / 3)); break; case "month": case "mm": case "m": DateDiffVal = (Int64)(((cal.GetYear(EndDate) - cal.GetYear(StartDate)) * 12 + cal.GetMonth(EndDate)) - cal.GetMonth(StartDate)); break; case "day": case "d": case "dd": DateDiffVal = (Int64)ts.TotalDays; break; case "week": case "wk": case "ww": DateDiffVal = (Int64)(ts.TotalDays / 7); break; case "hour": case "hh": DateDiffVal = (Int64)ts.TotalHours; break; case "minute": case "mi": case "n": DateDiffVal = (Int64)ts.TotalMinutes; break; case "second": case "ss": case "s": DateDiffVal = (Int64)ts.TotalSeconds; break; case "millisecond": case "ms": DateDiffVal = (Int64)ts.TotalMilliseconds; break; default: throw new Exception(String.Format("DatePart \"{0}\" is unknown", DatePart)); } return(DateDiffVal); }
/// <summary> /// DateDiff in SQL style. /// Datepart implemented: /// "year" (abbr. "yy", "yyyy"), /// "quarter" (abbr. "qq", "q"), /// "month" (abbr. "mm", "m"), /// "day" (abbr. "dd", "d"), /// "week" (abbr. "wk", "ww"), /// "hour" (abbr. "hh"), /// "minute" (abbr. "mi", "n"), /// "second" (abbr. "ss", "s"), /// "millisecond" (abbr. "ms"). /// </summary> /// <param name="startDate">The start date.</param> /// <param name="endDate">The end date.</param> /// <param name="datePary">The date part.</param> /// <returns>Date Difference in System.long</returns> /// <exception cref="Exception"></exception> public static long DateDiff(this DateTime startDate, DateTime endDate, string datePary) { long DateDiffVal = 0; System.Globalization.Calendar cal = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; TimeSpan ts = new TimeSpan(endDate.Ticks - startDate.Ticks); switch (datePary.ToLower().Trim()) { #region year case "year": case "yy": case "yyyy": DateDiffVal = (long)(cal.GetYear(endDate) - cal.GetYear(startDate)); break; #endregion #region quarter case "quarter": case "qq": case "q": DateDiffVal = (long)((((cal.GetYear(endDate) - cal.GetYear(startDate)) * 4) + ((cal.GetMonth(endDate) - 1) / 3)) - ((cal.GetMonth(startDate) - 1) / 3)); break; #endregion #region month case "month": case "mm": case "m": DateDiffVal = (long)(((cal.GetYear(endDate) - cal.GetYear(startDate)) * 12 + cal.GetMonth(endDate)) - cal.GetMonth(startDate)); break; #endregion #region day case "day": case "d": case "dd": DateDiffVal = (long)ts.TotalDays; break; #endregion #region week case "week": case "wk": case "ww": DateDiffVal = (long)(ts.TotalDays / 7); break; #endregion #region hour case "hour": case "hh": DateDiffVal = (long)ts.TotalHours; break; #endregion #region minute case "minute": case "mi": case "n": DateDiffVal = (long)ts.TotalMinutes; break; #endregion #region second case "second": case "ss": case "s": DateDiffVal = (long)ts.TotalSeconds; break; #endregion #region millisecond case "millisecond": case "ms": DateDiffVal = (long)ts.TotalMilliseconds; break; #endregion default: throw new Exception(string.Format("DatePart \"{0}\" is unknown", datePary)); } return(DateDiffVal); }
protected override void Render(HtmlTextWriter writer) { try { DateTime tmpDate; try { tmpDate = this.SelectedDate == "" ? DateTime.Now : Convert.ToDateTime(SelectedDate); } catch (Exception ex) { tmpDate = DateTime.Now; } string temp = CssClass; CssClass = ""; if (temp == "") { temp = "ampicker"; } writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0"); writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0"); writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.ToString()); writer.RenderBeginTag(HtmlTextWriterTag.Table); writer.RenderBeginTag(HtmlTextWriterTag.Tr); if (Text != "") { writer.AddAttribute(HtmlTextWriterAttribute.Style, "white-space:nowrap"); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.Write(Text); writer.RenderEndTag(); } writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.ToString()); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.AddAttribute("class", temp); writer.AddAttribute("id", ClientID); writer.AddAttribute("name", ClientID); writer.AddAttribute("onblur", "return window." + ClientID + ".onblur(this);"); writer.AddAttribute("onkeypress", "return window." + ClientID + ".onlyDateChars(event);"); //writer.AddAttribute("onkeydown", "return window." & Me.ClientID & ".KeyPress(event);") //writer.AddAttribute("onclick", "return window." & Me.ClientID & ".Click(event);showalert();") if (Enabled == false) { writer.AddAttribute("disabled", "disabled"); } if (ShowDateBox) { writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); } dtFI = Thread.CurrentThread.CurrentCulture.DateTimeFormat; if (!(string.IsNullOrEmpty(SelectedDate))) { DateTime dte = DateTime.Parse(SelectedDate); SelectedDate = dte.ToString(dtFI.ShortDatePattern + " " + dtFI.ShortTimePattern); } writer.AddAttribute("type", "hidden"); writer.AddAttribute("id", "hid_" + ClientID); writer.AddAttribute("name", "hid_" + ClientID); writer.AddAttribute("value", SelectedDate); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); writer.AddAttribute("id", "cal_" + ClientID); writer.AddAttribute("style", "display:none;position:absolute;"); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderBeginTag(HtmlTextWriterTag.Td); if (ImageUrl == string.Empty) { ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), "DotNetNuke.Modules.ActiveForums.CustomControls.Resources.calendar.gif"); } if (Enabled) { writer.AddAttribute("src", ImageUrl); writer.AddAttribute("onclick", "window." + ClientID + ".Toggle(event);"); writer.AddAttribute("id", "img_" + ClientID); writer.RenderBeginTag(HtmlTextWriterTag.Img); writer.RenderEndTag(); } writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderEndTag(); var str = new StringBuilder(); str.Append("<script type=\"text/javascript\">"); cal = new System.Globalization.GregorianCalendar(); if (Thread.CurrentThread.CurrentCulture != null) { cal = Thread.CurrentThread.CurrentCulture.Calendar; } DateFormat = dtFI.ShortDatePattern; TimeFormat = dtFI.ShortTimePattern; str.Append("window." + ClientID + "=new asDatePicker('" + ClientID + "');"); str.Append("window." + ClientID + ".Locale='" + Context.Request.UserLanguages[0].Substring(0, 2).ToUpper() + "';"); str.Append("window." + ClientID + ".SelectedDate='" + SelectedDate + "';"); str.Append("window." + ClientID + ".Width='" + CalendarWidth + "';"); str.Append("window." + ClientID + ".Height='" + CalendarHeight + "';"); str.Append("window." + ClientID + ".DateFormat='" + dtFI.ShortDatePattern + "';"); str.Append("window." + ClientID + ".TimeFormat='" + dtFI.ShortTimePattern + "';"); str.Append("window." + ClientID + ".Year=" + tmpDate.Year + ";"); str.Append("window." + ClientID + ".Month=" + (tmpDate.Month - 1) + ";"); str.Append("window." + ClientID + ".Day=" + tmpDate.Day + ";"); str.Append("window." + ClientID + ".SelectedYear=" + tmpDate.Year + ";"); str.Append("window." + ClientID + ".SelectedMonth=" + (tmpDate.Month - 1) + ";"); str.Append("window." + ClientID + ".SelectedDay=" + tmpDate.Day + ";"); str.Append("window." + ClientID + ".ShowTime=" + ShowTime.ToString().ToLower() + ";"); str.Append("window." + ClientID + ".DefaultTime='" + DefaultTime + "';"); str.Append("window." + ClientID + ".CallbackFlag='" + CallbackFlag + "';"); if (!(string.IsNullOrEmpty(RelatedControl))) { Control ctl = Parent.FindControl(RelatedControl); if (ctl == null) { ctl = Page.FindControl(RelatedControl); } if (ctl == null) { RelatedControl = string.Empty; } else { RelatedControl = ctl.ClientID; } } str.Append("window." + ClientID + ".linkedControl='" + RelatedControl + "';"); if (IsEndDate) { str.Append("window." + ClientID + ".isEndDate=true;"); } else { str.Append("window." + ClientID + ".isEndDate=false;"); } string sTime = string.Empty; SelectedTime = tmpDate.ToString(TimeFormat); if (ShowTime) { if (SelectedTime != "12:00 AM") { sTime = SelectedTime; } if (TimeRequired) { str.Append("window." + ClientID + ".RequireTime=true;"); } else { str.Append("window." + ClientID + ".RequireTime=false;"); } } else { str.Append("window." + ClientID + ".RequireTime=false;"); } str.Append("window." + ClientID + ".SelectedTime='" + sTime + "';"); if (string.IsNullOrEmpty(ImgNext)) { str.Append("window." + ClientID + ".ImgNext='" + Page.ClientScript.GetWebResourceUrl(GetType(), "DotNetNuke.Modules.ActiveForums.CustomControls.Resources.cal_nextMonth.gif") + "';"); } else { str.Append("window." + ClientID + ".ImgNext='" + Page.ResolveUrl(ImgNext) + "';"); } if (string.IsNullOrEmpty(ImgPrev)) { str.Append("window." + ClientID + ".ImgPrev='" + Page.ClientScript.GetWebResourceUrl(GetType(), "DotNetNuke.Modules.ActiveForums.CustomControls.Resources.cal_prevMonth.gif") + "';"); } else { str.Append("window." + ClientID + ".ImgPrev='" + Page.ResolveUrl(ImgPrev) + "';"); } if (SelectedDate != "") { try { if (ShowTime == false && sTime == string.Empty) { str.Append("window." + ClientID + ".textbox.value=new Date(" + tmpDate.Year + "," + (tmpDate.Month - 1) + "," + tmpDate.Day + ").formatDP('" + DateFormat + "','" + ClientID + "');"); str.Append("window." + ClientID + ".dateSel = new Date(" + tmpDate.Year + "," + (tmpDate.Month - 1) + "," + tmpDate.Day + ",0,0,0,0);"); } else { str.Append("window." + ClientID + ".textbox.value=new Date(" + tmpDate.Year + "," + (tmpDate.Month - 1) + "," + tmpDate.Day + "," + tmpDate.Hour + "," + tmpDate.Minute + ",0).formatDP('" + DateFormat + " " + TimeFormat + "','" + ClientID + "');"); str.Append("window." + ClientID + ".dateSel = new Date(" + tmpDate.Year + "," + (tmpDate.Month - 1) + "," + tmpDate.Day + "," + tmpDate.Hour + "," + tmpDate.Minute + ",0);"); } } catch (Exception ex) { } } int xMonths = cal.GetMonthsInYear(cal.GetYear(tmpDate), cal.GetEra(tmpDate)); int currMonth = cal.GetMonth(tmpDate); int currYear = cal.GetYear(tmpDate); int currDay = cal.GetDayOfMonth(tmpDate); str.Append("window." + ClientID + ".MonthDays = new Array("); for (int i = 0; i < xMonths; i++) { str.Append(cal.GetDaysInMonth(currYear, i + 1)); if (i < (xMonths - 1)) { str.Append(","); } } str.Append(");"); str.AppendLine(); string[] mNames = dtFI.MonthNames; str.Append("window." + ClientID + ".MonthNames = new Array("); for (int i = 0; i < xMonths; i++) { str.Append("'" + mNames[i] + "'"); if (i < (xMonths - 1)) { str.Append(","); } } str.Append(");"); str.AppendLine(); str.Append("window." + ClientID + ".ShortMonthNames = new Array("); string[] mAbbr = dtFI.AbbreviatedMonthNames; for (int i = 0; i < xMonths; i++) { str.Append("'" + mAbbr[i] + "'"); if (i < (xMonths - 1)) { str.Append(","); } } str.Append(");"); str.AppendLine(); str.Append("window." + ClientID + ".ShortDayNames = new Array("); string[] dAbbr = dtFI.AbbreviatedDayNames; for (int i = 0; i <= 6; i++) { str.Append("'" + dAbbr[i] + "'"); if (i < 6) { str.Append(","); } } str.Append(");"); str.AppendLine(); str.Append("window." + ClientID + ".Class={"); str.Append("CssCalendarStyle:'" + CssCalendarStyle + "',"); str.Append("CssMonthStyle:'" + CssMonthStyle + "',"); str.Append("CssWeekendStyle:'" + CssWeekendStyle + "',"); str.Append("CssWeekdayStyle:'" + CssWeekdayStyle + "',"); str.Append("CssSelectedDayStyle:'" + CssSelectedDayStyle + "',"); str.Append("CssCurrentMonthDayStyle:'" + CssCurrentMonthDayStyle + "',"); str.Append("CssOtherMonthDayStyle:'" + CssOtherMonthDayStyle + "',"); str.Append("CssDayHeaderStyle:'" + CssDayHeaderStyle + "',"); str.Append("CssCurrentDayStyle:'" + CssCurrentDayStyle + "'};"); str.Append("window." + ClientID + ".selectedDate=window." + ClientID + ".textbox.value;"); str.Append("window." + ClientID + ".timeLabel='[RESX:Time]';"); str.Append("</script>"); writer.Write(str); } catch (Exception ex) { } }