欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net 时间操作基类(支持短日期,长日期,时间差)

程序员文章站 2022-06-08 17:43:13
复制代码 代码如下:using system; using system.data; using system.configuration; using system.we...

复制代码 代码如下:

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 ec
{
/// <summary>
/// 时间操作函数
/// </summary>
public class timeobject
{
/// <summary>
/// 时间差
/// </summary>
/// <param name="starttime">开始时间</param>
/// <param name="endtime">结束时间</param>
/// <returns></returns>
public static string gettimespan(datetime starttime, datetime endtime)
{
timespan ts = endtime - starttime;
return string.format("{0}时{1}分{2}秒{3}毫秒", ts.hours, ts.minutes, ts.seconds, ts.milliseconds);
}
/// <summary>
/// 转称为月-日:09-12
/// </summary>
/// <param name="d">日期</param>
/// <returns></returns>
public static string todatemonthday(datetime d)
{
return d.tostring("mm-dd");
}
/// <summary>
/// 转换为年月日:2006-09-12
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static string todatestring(datetime d)
{
return d.tostring("yyyy-mm-dd");
}
/// <summary>
/// 转换为:小时:分:秒 00:29:58
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string totimestring(datetime t)
{
return t.tostring("hh:mm:ss");
}
/// <summary>
/// 转换为长时间:2006-09-12 00:30:57
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string todatetimestring(datetime dt)
{
return dt.tostring("yyyy-mm-dd hh:mm:ss");
}
}
}