asp.net(C#)常用时间日期处理类
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
namespace mscl
{
/// <summary>
/// 时间日期处理类
/// </summary>
public class datetimehelper
{
#region 返回本年有多少天
/// <summary>返回本年有多少天</summary>
/// <param name="iyear">年份</param>
/// <returns>本年的天数</returns>
public static int getdaysofyear(int iyear)
{
int cnt = 0;
if (isruyear(iyear))
{
//闰年多 1 天 即:2 月为 29 天
cnt = 366;
}
else
{
//--非闰年少1天 即:2 月为 28 天
cnt = 365;
}
return cnt;
}
#endregion
#region 返回本年有多少天
/// <summary>本年有多少天</summary>
/// <param name="dt">日期</param>
/// <returns>本天在当年的天数</returns>
public static int getdaysofyear(datetime idt)
{
int n;
//取得传入参数的年份部分,用来判断是否是闰年
n = idt.year;
if (isruyear(n))
{
//闰年多 1 天 即:2 月为 29 天
return 366;
}
else
{
//--非闰年少1天 即:2 月为 28 天
return 365;
}
}
#endregion
#region 返回本月有多少天
/// <summary>本月有多少天</summary>
/// <param name="iyear">年</param>
/// <param name="month">月</param>
/// <returns>天数</returns>
public static int getdaysofmonth(int iyear, int month)
{
int days = 0;
switch (month)
{
case 1:
days = 31;
break;
case 2:
if (isruyear(iyear))
{
//闰年多 1 天 即:2 月为 29 天
days = 29;
}
else
{
//--非闰年少1天 即:2 月为 28 天
days = 28;
}
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
}
return days;
}
#endregion
#region 返回本月有多少天
/// <summary>本月有多少天</summary>
/// <param name="dt">日期</param>
/// <returns>天数</returns>
public static int getdaysofmonth(datetime dt)
{
//--------------------------------//
//--从dt中取得当前的年,月信息 --//
//--------------------------------//
int year, month, days = 0;
year = dt.year;
month = dt.month;
//--利用年月信息,得到当前月的天数信息。
switch (month)
{
case 1:
days = 31;
break;
case 2:
if (isruyear(year))
{
//闰年多 1 天 即:2 月为 29 天
days = 29;
}
else
{
//--非闰年少1天 即:2 月为 28 天
days = 28;
}
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
}
return days;
}
#endregion
#region 返回当前日期的星期名称
/// <summary>返回当前日期的星期名称</summary>
/// <param name="dt">日期</param>
/// <returns>星期名称</returns>
public static string getweeknameofday(datetime idt)
{
string dt, week = "";
dt = idt.dayofweek.tostring();
switch (dt)
{
case "mondy":
week = "星期一";
break;
case "tuesday":
week = "星期二";
break;
case "wednesday":
week = "星期三";
break;
case "thursday":
week = "星期四";
break;
case "friday":
week = "星期五";
break;
case "saturday":
week = "星期六";
break;
case "sunday":
week = "星期日";
break;
}
return week;
}
#endregion
#region 返回当前日期的星期编号
/// <summary>返回当前日期的星期编号</summary>
/// <param name="dt">日期</param>
/// <returns>星期数字编号</returns>
public static string getweeknumberofday(datetime idt)
{
string dt, week = "";
dt = idt.dayofweek.tostring();
switch (dt)
{
case "mondy":
week = "1";
break;
case "tuesday":
week = "2";
break;
case "wednesday":
week = "3";
break;
case "thursday":
week = "4";
break;
case "friday":
week = "5";
break;
case "saturday":
week = "6";
break;
case "sunday":
week = "7";
break;
}
return week;
}
#endregion
#region 判断当前日期所属的年份是否是闰年,私有函数
/// <summary>判断当前日期所属的年份是否是闰年,私有函数</summary>
/// <param name="dt">日期</param>
/// <returns>是闰年:true ,不是闰年:false</returns>
private static bool isruyear(datetime idt)
{
//形式参数为日期类型
//例如:2003-12-12
int n;
n = idt.year;
if ((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 判断当前年份是否是闰年,私有函数
/// <summary>判断当前年份是否是闰年,私有函数</summary>
/// <param name="dt">年份</param>
/// <returns>是闰年:true ,不是闰年:false</returns>
private static bool isruyear(int iyear)
{
//形式参数为年份
//例如:2003
int n;
n = iyear;
if ((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
{
return true;
}
else
{
return false;
}
}
#endregion
#region 将输入的字符串转化为日期。如果字符串的格式非法,则返回当前日期
/// <summary>
/// 将输入的字符串转化为日期。如果字符串的格式非法,则返回当前日期。
/// </summary>
/// <param name="strinput">输入字符串</param>
/// <returns>日期对象</returns>
public static datetime convertstringtodate(string strinput)
{
datetime odatetime;
try
{
odatetime = datetime.parse(strinput);
}
catch (exception)
{
odatetime = datetime.today;
}
return odatetime;
}
#endregion
#region 将日期对象转化为格式字符串
/// <summary>
/// 将日期对象转化为格式字符串
/// </summary>
/// <param name="odatetime">日期对象</param>
/// <param name="strformat">
/// 格式:
/// "shortdate"===短日期
/// "longdate"==长日期
/// 其它====自定义格式
/// </param>
/// <returns>日期字符串</returns>
public static string convertdatetostring(datetime odatetime, string strformat)
{
string strdate = "";
try
{
switch (strformat.toupper())
{
case "shortdate":
strdate = odatetime.toshortdatestring();
break;
case "longdate":
strdate = odatetime.tolongdatestring();
break;
default:
strdate = odatetime.tostring(strformat);
break;
}
}
catch (exception)
{
strdate = odatetime.toshortdatestring();
}
return strdate;
}
#endregion
#region 判断是否为合法日期,必须大于1800年1月1日
/// <summary>
/// 判断是否为合法日期,必须大于1800年1月1日
/// </summary>
/// <param name="strdate">输入日期字符串</param>
/// <returns>true/false</returns>
public static bool isdatetime(string strdate)
{
try
{
datetime odate = datetime.parse(strdate);
if (odate.compareto(datetime.parse("1800-1-1")) > 0)
return true;
else
return false;
}
catch (exception)
{
return false;
}
}
#endregion
#region 获取两个日期之间的差值 可返回年 月 日 小时 分钟 秒
/// <summary>
/// 获取两个日期之间的差值
/// </summary>
/// <param name="howtocompare">比较的方式可为:year month day hour minute second</param>
/// <param name="startdate">开始日期</param>
/// <param name="enddate">结束日期</param>
/// <returns>时间差</returns>
public static double datediff(string howtocompare, datetime startdate, datetime enddate)
{
double diff = 0;
try
{
timespan ts = new timespan(enddate.ticks - startdate.ticks);
switch (howtocompare.tolower())
{
case "year":
diff = convert.todouble(ts.totaldays / 365);
break;
case "month":
diff = convert.todouble((ts.totaldays / 365) * 12);
break;
case "day":
diff = convert.todouble(ts.totaldays);
break;
case "hour":
diff = convert.todouble(ts.totalhours);
break;
case "minute":
diff = convert.todouble(ts.totalminutes);
break;
case "second":
diff = convert.todouble(ts.totalseconds);
break;
}
}
catch (exception)
{
diff = 0;
}
return diff;
}
#endregion
#region 计算两个日期之间相差的工作日天数
/// <summary>
/// 计算两个日期之间相差的工作日天数
/// </summary>
/// <param name="dtstart">开始日期</param>
/// <param name="dtend">结束日期</param>
/// <param name="flag">是否除去周六,周日</param>
/// <returns>int</returns>
public static int calculateworkingdays(datetime dtstart, datetime dtend, bool flag)
{
int count = 0;
for (datetime dttemp = dtstart; dttemp < dtend; dttemp = dttemp.adddays(1))
{
if (flag)
{
if (dttemp.dayofweek != dayofweek.saturday && dttemp.dayofweek != dayofweek.sunday)
{
count++;
}
}
else
{
count++;
}
}
return count;
}
#endregion
}
上一篇: Oracle 数据库操作技巧集
推荐阅读
-
C# 常用日期时间函数(老用不熟)
-
整理总结 python 中时间日期类数据处理与类型转换(含 pandas)
-
asp.net(C#)常用时间日期处理类
-
asp.net 时间类 一周的周一和周末的日期
-
Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
-
Asp.net 时间操作基类(支持短日期,长日期,时间差)
-
Python常用的日期时间处理方法示例
-
asp.net(c#) ubb处理类
-
Asp.net(c#)常用文件操作类封装 移动 复制 删除 上传 下载等
-
MySQL数据库文本处理函数、常用日期和时间处理函数、数值处理函数讲解