You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
420 lines
9.0 KiB
420 lines
9.0 KiB
3 years ago
|
/*******************************************************/
|
||
|
/*Project:基础类库
|
||
|
Module :时间函数库
|
||
|
Description :
|
||
|
Date : 2008-6-3 15:09:12
|
||
|
Create : Lxc
|
||
|
Update : 2015-12-31
|
||
|
TODO : */
|
||
|
/*******************************************************/
|
||
|
|
||
|
namespace System
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 日期时间操作单元
|
||
|
/// </summary>
|
||
|
public partial class DateUnit
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 1970-1-1
|
||
|
/// </summary>
|
||
|
public static DateTime InitDate()
|
||
|
{
|
||
|
return new DateTime(1970, 1, 1);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 默认当前日期
|
||
|
/// </summary>
|
||
|
public static DateTime DefDate()
|
||
|
{
|
||
|
return DateTime.Now;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 格式化后的当前日期
|
||
|
/// </summary>
|
||
|
public static string DefFormatDate()
|
||
|
{
|
||
|
return FormatDate(DefDate());
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
///格式化当前时间
|
||
|
/// </summary>
|
||
|
public static string DefFormatDateTime()
|
||
|
{
|
||
|
return FormatDateTime(DefDate());
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 格式化后的 当前月1号 日期格式yyyy-MM-01
|
||
|
/// </summary>
|
||
|
public static string DefMonthBegFormat()
|
||
|
{
|
||
|
return DefDate().ToString("yyyy-MM-01");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 字符串格式->格式化为当前日期
|
||
|
/// </summary>
|
||
|
/// <param name="Indate"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string FormatDate(string Indate)
|
||
|
{
|
||
|
DateTime nowDate = DefDate();
|
||
|
|
||
|
if (Indate != null && Indate != "")
|
||
|
{
|
||
|
nowDate = ToDate(Indate);
|
||
|
}
|
||
|
|
||
|
return FormatDate(nowDate);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 格式化为 日期格式yyyy-MM-dd
|
||
|
/// </summary>
|
||
|
/// <param name="Indate"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string FormatDate(DateTime Indate)
|
||
|
{
|
||
|
if (!Indate.Equals(null))
|
||
|
{
|
||
|
return Indate.ToString("yyyy-MM-dd");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return DefFormatDate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 格式化为 日期时间格式yyyy-MM-dd HH:mm:ss
|
||
|
/// </summary>
|
||
|
/// <param name="Indate"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string FormatDateTime(DateTime Indate)
|
||
|
{
|
||
|
if (!Indate.Equals(null))
|
||
|
{
|
||
|
return Indate.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return DefFormatDate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 格式化时间 毫秒
|
||
|
/// </summary>
|
||
|
/// <param name="Indate"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string FormatDateTimefff(DateTime Indate)
|
||
|
{
|
||
|
if (!Indate.Equals(null))
|
||
|
{
|
||
|
return Indate.ToString("yyyy-MM-dd HH:mm:ss fff");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return DefFormatDate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#region 时间函数
|
||
|
|
||
|
/// <summary>
|
||
|
/// 是不是时间格式
|
||
|
/// </summary>
|
||
|
/// <param name="datetm"></param>
|
||
|
/// <returns></returns>
|
||
|
public static bool IsDate(string datetm)
|
||
|
{
|
||
|
bool result = false;
|
||
|
try
|
||
|
{
|
||
|
DateTime dateTime = Convert.ToDateTime(datetm);
|
||
|
result = true;
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
result = false;
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 将字符串转化成日期,如果输入的字符串不是日期时,则返回1970-1-1
|
||
|
/// </summary>
|
||
|
/// <param name="str"></param>
|
||
|
/// <returns></returns>
|
||
|
public static DateTime ToDate(string str)
|
||
|
{
|
||
|
if (str == null || str == "")
|
||
|
return DefDate();
|
||
|
else
|
||
|
{
|
||
|
DateTime dt = DefDate();
|
||
|
if (DateTime.TryParse(str, out dt) == false)
|
||
|
return DefDate();
|
||
|
else
|
||
|
return dt;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 将对象转化成日期,如果输入的对象不是日期时,则返回1970-1-1
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
/// <returns></returns>
|
||
|
public static DateTime ToDate(object obj)
|
||
|
{
|
||
|
DateTime Result = DefDate();
|
||
|
if (!obj.Equals(null))
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Result = (DateTime)obj;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
return DefDate();
|
||
|
}
|
||
|
//if (DateTime.TryParse(obj.ToString(), out Result) == false)
|
||
|
// return DefDate;
|
||
|
}
|
||
|
return Result;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 将对象转化成日期,如果输入的对象不是日期时,则返回1900-1-1
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
/// <returns></returns>
|
||
|
public static DateTime ToDate(System.Nullable<System.DateTime> obj)
|
||
|
{
|
||
|
if (!obj.Equals(null))
|
||
|
{
|
||
|
return (DateTime)obj;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return DefDate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 20070101-->2007-01-01
|
||
|
/// </summary>
|
||
|
/// <param name="Str"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string StrTodate(string Str)
|
||
|
{
|
||
|
string stryear, strmon, strday;
|
||
|
try
|
||
|
{
|
||
|
stryear = Str.Remove(4, 4);
|
||
|
Str = Str.Remove(0, 4);
|
||
|
strmon = Str.Remove(2, 2);
|
||
|
strday = Str.Remove(0, 2);
|
||
|
stryear = stryear + "-" + strmon + "-" + strday;
|
||
|
return stryear;
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
return DefFormatDate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion 时间函数
|
||
|
|
||
|
#region 周计算
|
||
|
|
||
|
/// <summary>
|
||
|
/// 周几
|
||
|
/// </summary>
|
||
|
/// <param name="curDay"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string DayOfWeek(DateTime curDay)
|
||
|
{
|
||
|
string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
|
||
|
string week = weekdays[Convert.ToInt32(curDay.DayOfWeek)];
|
||
|
|
||
|
return week;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 第几周
|
||
|
/// </summary>
|
||
|
/// <param name="curDay"></param>
|
||
|
/// <returns></returns>
|
||
|
public static int WeekOfYear(DateTime curDay)
|
||
|
{
|
||
|
int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "-" + "1-1").DayOfWeek);
|
||
|
|
||
|
int days = curDay.DayOfYear;
|
||
|
int daysOutOneWeek = days - (7 - firstdayofweek);
|
||
|
|
||
|
if (daysOutOneWeek <= 0)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int weeks = daysOutOneWeek / 7;
|
||
|
if (daysOutOneWeek % 7 != 0)
|
||
|
weeks++;
|
||
|
|
||
|
return weeks + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#region 返回某年某月最后一天
|
||
|
|
||
|
/// <summary>
|
||
|
/// 返回某年某月最后一天
|
||
|
/// </summary>
|
||
|
/// <param name="year">年份</param>
|
||
|
/// <param name="month">月份</param>
|
||
|
/// <returns>日</returns>
|
||
|
public static int GetMonthLastDate(int year, int month)
|
||
|
{
|
||
|
DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
|
||
|
int Day = lastDay.Day;
|
||
|
return Day;
|
||
|
}
|
||
|
|
||
|
#endregion 返回某年某月最后一天
|
||
|
|
||
|
#endregion 周计算
|
||
|
|
||
|
#region 返回时间差
|
||
|
|
||
|
/// <summary>
|
||
|
/// 返回时间差 秒
|
||
|
/// </summary>
|
||
|
/// <param name="begDateTime"></param>
|
||
|
/// <param name="endDateTime"></param>
|
||
|
/// <returns></returns>
|
||
|
public static double DateDiffSeconds(DateTime begDateTime, DateTime endDateTime)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
TimeSpan ts = endDateTime - begDateTime;
|
||
|
return ts.TotalSeconds;
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 返回时间差 分钟
|
||
|
/// </summary>
|
||
|
/// <param name="begDateTime"></param>
|
||
|
/// <param name="endDateTime"></param>
|
||
|
/// <returns></returns>
|
||
|
public static double DateDiffMinutes(DateTime begDateTime, DateTime endDateTime)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
TimeSpan ts = endDateTime - begDateTime;
|
||
|
return ts.TotalMinutes;
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static double DateDiffHours(DateTime begDateTime, DateTime endDateTime)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
TimeSpan ts = endDateTime - begDateTime;
|
||
|
return ts.TotalHours;
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static double DateDifDays(DateTime begDateTime, DateTime endDateTime)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
TimeSpan ts = endDateTime - begDateTime;
|
||
|
return ts.TotalDays;
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion 返回时间差
|
||
|
|
||
|
/// <summary>
|
||
|
/// Unix时间戳
|
||
|
/// </summary>
|
||
|
/// <returns>毫秒</returns>
|
||
|
public static long UnixTime()
|
||
|
{
|
||
|
System.DateTime time = DateTime.Now;
|
||
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
|
||
|
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 将c# DateTime时间格式转换为Unix时间戳格式
|
||
|
/// </summary>
|
||
|
/// <param name="time">时间</param>
|
||
|
/// <returns> 毫秒</returns>
|
||
|
public static long DateTimeToUnixTime(System.DateTime time)
|
||
|
{
|
||
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
|
||
|
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 时间戳转为C#格式时间
|
||
|
/// </summary>
|
||
|
/// <param name="timeStamp"></param>
|
||
|
/// <returns></returns>
|
||
|
public static DateTime UnixTimeToDateTime(long timeStamp)
|
||
|
{
|
||
|
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
|
long lTime = timeStamp * 10000;
|
||
|
TimeSpan toNow = new TimeSpan(lTime);
|
||
|
return dtStart.Add(toNow);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Unix时间戳 时间格式化 为日期
|
||
|
/// </summary>
|
||
|
/// <param name="timeStamp"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string FormatDateUnixTime(long timeStamp)
|
||
|
{
|
||
|
DateTime dtStart = UnixTimeToDateTime(timeStamp);
|
||
|
return FormatDate(dtStart);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Unix时间戳 时间格式化 为时间
|
||
|
/// </summary>
|
||
|
/// <param name="timeStamp"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string FormatDateTimeUnixTime(long timeStamp)
|
||
|
{
|
||
|
DateTime dtStart = UnixTimeToDateTime(timeStamp);
|
||
|
return FormatDateTime(dtStart);
|
||
|
}
|
||
|
}
|
||
|
}
|