首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

通過datatime.now()得到相應的開始日期和結束日期。该如何处理

2012-01-01 
通過datatime.now()得到相應的開始日期和結束日期。starttime:textboxendtime:textboxPage_load()1.得到该

通過datatime.now()得到相應的開始日期和結束日期。
starttime:textbox             endtime:textbox  

Page_load()
1.得到该周一時間和该周末的時間;
2.得到该月开始時間和该月结束時間;
3.得到该季度开始時間和该季度结束時間;
4.得到该年度开始時間和该年度结束時間;

  如何用C#实现.或Javascript实现。

[解决办法]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Globalization;

namespace Digdotnet
{
/// <summary>
/// 表示公历历法帮助类。
/// </summary>
public static class CalendarHelper
{
public const int DaysOfFullLeapYear = 366;
public const int DaysOfFullInleapYear = 365;
public const int DaysOfSecondHalfYear = 184;
public const int DaysInWeek = 7;

private readonly static DateTime InternalMinDate = new DateTime(1900, 1, 1);
private readonly static Calendar InternalCalendar = Calendar.ReadOnly(new GregorianCalendar());

//DateTime now = DateTime.Now;

#region

public static HalfYearKind GetHalfYearOfYear(DateTime time)
{
return GetHalfYearOfYear(time.Month);
}

public static HalfYearKind GetHalfYearOfYear(QuarterKind quarter)
{
if (quarter == QuarterKind.First || quarter == QuarterKind.Second) {
return HalfYearKind.FirstHalf;
}
else {
return HalfYearKind.SecondHalf;
}
}

public static HalfYearKind GetHalfYearOfYear(int month)
{
if (month < 1 || month > 12) {
throw new ArgumentOutOfRangeException( "month ", month, "参数必须大于0且小于13。 ");
}

return (HalfYearKind)(month / 7 + 1);
}

public static QuarterKind GetQuarterOfYear(DateTime time)
{
return GetQuarterOfYear((byte)time.Month);
}

public static QuarterKind GetQuarterOfYear(int month)
{
if (month < 1 || month > 12) {
throw new ArgumentOutOfRangeException( "month ", month, "参数必须大于0且小于13。 ");
}

if (month > 7) {
month += 2;
}

return (QuarterKind)(month / 4 + 1);
}

public static TendaysKind GetTendaysOfMonth(DateTime time)
{
return GetTendaysOfMonth((byte)time.Day);
}

public static TendaysKind GetTendaysOfMonth(int dayOfMonth)
{
if (dayOfMonth < 1 || dayOfMonth > 31) {
throw new ArgumentOutOfRangeException( "dayOfMonth ", dayOfMonth, "参数必须大于0且小于32。 ");
}

if (dayOfMonth > 20) {
dayOfMonth++;
}

return (TendaysKind)(dayOfMonth / 11 + 1);
}

/// <summary>
/// 返回指定半年间的第一个月。
/// </summary>
/// <param name= "value "> </param>
/// <returns> </returns>
public static int GetFirstMonthOfHalfYear(HalfYearKind value)
{
return (value == HalfYearKind.FirstHalf) ? 1 : 7;
}

/// <summary>
/// 返回指定半年间的最后一个月。


/// </summary>
/// <param name= "value "> </param>
/// <returns> </returns>
public static int GetLastMonthOfHalfYear(HalfYearKind value)
{
return (value == HalfYearKind.FirstHalf) ? 6 : 12;
}

public static int GetFirstMonthOfQuarter(QuarterKind value)
{
return ((int)value - 1) * 3 + 1;
}

public static int GetLastMonthOfQuarter(QuarterKind value)
{
return ((int)value - 1) * 3 + 3;
}

public static int GetFirstDayOfTendays(TendaysKind value)
{
return ((int)value - 1) * 10 + 1;
}

public static int GetLastDayOfTendays(int year, int month, TendaysKind value)
{
if (value == TendaysKind.Third) {
return GetLastDayOfMonth(year, month) - 20;
}
else {
return ((int)value - 1) * 10 + 10;
}
}

public static DayOfWeek GetLastDayOfWeek(DayOfWeek firstDayOfWeek)
{
return (DayOfWeek)(((int)(firstDayOfWeek + 6)) % 7);
}

#endregion

#region

public static int CalendarDiff(CalendarPart part, DateTime beginTime, DateTime endTime)
{
switch (part) {
case CalendarPart.Year:
return (beginTime.Year - endTime.Year);
case CalendarPart.HalfYear:
return GetHalfYears(beginTime) - GetHalfYears(endTime);
case CalendarPart.Quarter:
return GetQuarters(beginTime) - GetQuarters(endTime);
case CalendarPart.Month:
return GetMonths(beginTime) - GetMonths(endTime);
case CalendarPart.Tendays:
return GetTendays(beginTime) - GetTendays(endTime);
case CalendarPart.Week:
throw new NotImplementedException();
case CalendarPart.Day:
return GetDays(beginTime) - GetDays(endTime);
default:
throw new ArgumentOutOfRangeException( "part ", part, "未知的日历独立运算部件。 ");
}
}

#endregion

#region internal counts

private static int GetYears(DateTime time)
{
if (time.Year < InternalMinDate.Year) {
throw new ArgumentOutOfRangeException( "time ", time, "参数的年份必须大于等于 " + InternalMinDate.Year.ToString());
}

int years = time.Year - InternalMinDate.Year;
return years;
}

private static int GetHalfYears(DateTime time)
{
int years = GetYears(time);
return (years * 2 +(int)GetHalfYearOfYear(time));
}

private static int GetQuarters(DateTime time)
{
int years = GetYears(time);
return (years * 4 + (int)GetQuarterOfYear(time));
}

private static int GetMonths(DateTime time)
{
int years = GetYears(time);
return (years * 12 + time.Month);
}

private static int GetTendays(DateTime time)
{
int months = GetMonths(time);
return (months * 3 + (int)GetTendaysOfMonth(time));


}

private static int GetDays(DateTime time)
{
TimeSpan span = time.Subtract(InternalMinDate);
return (int)span.TotalDays;
}

private static int GetYears(int year)
{
if (year < InternalMinDate.Year) {
throw new ArgumentOutOfRangeException( "year ", year, "参数必须大于等于 " + InternalMinDate.Year.ToString());
}

int years = year - InternalMinDate.Year;
return years;
}

private static int GetHalfYears(int year, HalfYearKind halfYear)
{
int years = GetYears(year);
return (years * 2 +(int)halfYear);
}

private static int GetQuarters(int year, QuarterKind quarter)
{
int years = GetYears(year);
return (years * 4 + (int)quarter);
}

private static int GetMonths(int year, int month)
{
int years = GetYears(year);
return (years * 12 + month);
}

private static int GetTendays(int year, int month, TendaysKind tendays)
{
int months = GetMonths(year, month);
return (months * 3 + (int)tendays);
}

private static int GetDays(int year, int month, int day)
{
DateTime date = new DateTime(year, month, day);
TimeSpan span = date.Subtract(InternalMinDate);
return (int)span.TotalDays;
}

#endregion
[解决办法]
C#实现,试下

DateTime dt = DateTime.Now; //当前时间

DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString( "d "))); //该周周一
DateTime endWeek = startWeek.AddDays(6); //该周周日

DateTime startMonth = dt.AddDays(1 - dt.Day); //月初
DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days-1); //月末

DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1-dt.Day); //季度初
DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1); //季度末

DateTime startYear = new DateTime(dt.Year, 1, 1); //年初
DateTime endYear = new DateTime(dt.Year, 12, 31); //年末

热点排行