using ryCommon.Pram;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ryCommon
{
///
/// 日期时间的操作类
///
public class RyDate
{
///
/// 获取当前是周几,周一到周日,分别是1-7.
///
///
///
public static int GetWeek_index(DateTime dt)
{
int week_index = dt.DayOfWeek.ToInt(); if (week_index == 0) { week_index = 7; }//获取当前是周几
return week_index;
}
///
/// 获取当前是周几,1-7,分别是返回一到日.
///
///
///
public static string GetWeekName(int index)
{
string[] week = "一,二,三,四,五,六,日".Split(',');
if (index.IsInRange(1, 7))
{
return week[index - 1];
}
else
return "" ;
}
///
/// 获取当前是周几,输入日期,根据周几,分别是返回一到日.
///
///
///
public static string GetWeekName(DateTime dt)
{
return GetWeekName(GetWeek_index(dt));
}
///
/// 获取一周的开始和结束,开始时间为第一天的0点,结束时间以最后一天的0点为结束时间
///
///
///
public static WeekInfo GetWeekInfo(DateTime dt)
{
int week_index = GetWeek_index(dt);//获取当前是周几
WeekInfo info = new WeekInfo() { startDate = dt.Date.AddDays(-week_index + 1), endDate = dt.Date.AddDays(7 - week_index) };
return info;
}
///
/// 获取2个日期相差几周
///
///
///
///
public static int GetWeekCount(DateTime dt1, DateTime dt2)
{
return (GetWeekInfo(dt2).startDate - GetWeekInfo(dt1).startDate).TotalDays.ToInt() / 7;
}
///
/// 获取2个日期相差几个月
///
///
///
///
public static int GetMonthCount(DateTime dt1, DateTime dt2)
{
return dt2.Year * 12 + dt2.Month - dt1.Year * 12 - dt1.Month;
}
///
/// 获取一个月的开始
///
///
///
public static DateTime GetMonthStart(DateTime dt)
{
return dt.AddDays(-dt.Day+1).Date;
}
///
/// 判断是否在同一个星期。
///
///
///
///
public static bool InSameWeek(DateTime dt1, DateTime dt2)
{
return GetWeekInfo(dt2).startDate==GetWeekInfo(dt1).startDate;
}
///
/// 判断是否在同一个月。
///
///
///
///
public static bool InSameMonth(DateTime dt1, DateTime dt2)
{
return dt2.Year==dt1.Year && dt2.Month==dt1.Month;
}
///
/// 判断是否在同一天
///
///
///
///
public static bool InSameDay(DateTime dt1, DateTime dt2)
{
return dt2.Date == dt1.Date;
}
///
/// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
///
/// 第一个日期和时间
/// 第二个日期和时间
///
public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
return DateDiff(DateTime1, DateTime2, true);
}
///
/// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
///
/// 第一个日期和时间
/// 第二个日期和时间
/// 是否显示秒
///
public static string DateDiff(DateTime DateTime1, DateTime DateTime2,bool seconds)
{
string dateDiff = "";
try
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
if (ts.Days != 0)
{
dateDiff = ts.Days.ToString() + "天";
}
if (ts.Hours != 0)
{
dateDiff += ts.Hours.ToString() + "小时";
}
if (ts.Minutes != 0)
{
dateDiff += ts.Minutes.ToString() + "分钟";
}
if (ts.Seconds != 0 && seconds)
{
dateDiff += ts.Seconds.ToString() + "秒";
}
}
catch
{
}
if (dateDiff == "") { dateDiff = "0"+ (seconds?"秒": "分钟"); }
return dateDiff;
}
///
/// 将秒数显示成中文表达式
///
///
///
public static string GetTimeStr(long Seconds)
{
return DateDiff(DateTime.MinValue, DateTime.MinValue.AddSeconds(Seconds), true);
}
///
/// 计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值.
///
/// 一个日期和时间
///
public static string DateDiff(DateTime DateTime1)
{
return DateDiff(DateTime1, DateTime.Now);
}
///
/// 将c# DateTime时间格式转换为Unix时间戳格式
///
/// 时间
/// long
public static long DateTimeToUnixTime(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(time - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}
///
/// 时间戳转为C#格式时间
///
///
///
public static DateTime UnixTimeToDateTime(string timeStamp)
{
try
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(timeStamp.ToDouble(0));
return dt;
}
catch { return new System.DateTime(1970, 1, 1); }
}
///
/// 时间戳转为C#格式时间
///
///
///
public static DateTime UnixTimeToDateTime(long timeStamp)
{
try
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(timeStamp);
return dt;
}
catch { return new System.DateTime(1970, 1, 1); }
}
///
/// 将c# DateTime时间格式转换为js时间戳格式
///
/// 时间
/// long
public static long DateTimeToJSTime(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(time - startTime).TotalMilliseconds; // 相差毫秒数
return timeStamp;
}
///
/// JS时间戳转为C#格式时间
///
///
///
public static DateTime JSTimeToDateTime(string timeStamp)
{
try
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddMilliseconds(timeStamp.ToInt64(0));
return dt;
}
catch { return new System.DateTime(1970, 1, 1); }
}
///
/// JS时间戳转为C#格式时间
///
///
///
public static DateTime JSTimeToDateTime(long timeStamp)
{
try
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddMilliseconds(timeStamp);
return dt;
}
catch { return new System.DateTime(1970, 1, 1); }
}
///
/// 根据年月日转换成日期
///
///
///
///
///
public static DateTime ToDate(int year, int month, int day)
{
try
{
return new DateTime(year, month, day);
}
catch { return new DateTime(2000, 1, 1); }
}
///
/// 根据日期和时间转换成日期时间
///
///
///
///
public static DateTime ToDateTime(DateTime date,DateTime time)
{
return new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second, time.Millisecond);
}
///
/// 将时间转换成当前分钟开始的时间
///
///
///
public static DateTime StartMinute(DateTime dt)
{
return dt.AddMilliseconds(-dt.Millisecond).AddSeconds(-dt.Second);
}
///
/// 将时间转换成当前秒钟开始的时间
///
///
///
public static DateTime StartSecond(DateTime dt)
{
return dt.AddMilliseconds(-dt.Millisecond);
}
///
/// 当前时间在这一天里的秒数
///
///
///
public static Int64 DaySecond(DateTime dt)
{
return dt.Hour*3600+dt.Minute*60+dt.Second;
}
}
}