You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/WebSqlHelper/dateHelper.cs

66 lines
2.2 KiB
C#

3 years ago
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
namespace WebSqlHelper
{
public class DateHelper
{
public static string dateFormat(string date, string format)
{
string str = "";
if (!string.IsNullOrEmpty(date))
{
try
{
str = DateTime.Parse(date).ToString(format);
}
catch
{ }
}
return str;
}
/// <summary>
/// 计算本周的周一日期
/// </summary>
/// <returns></returns>
public static DateTime GetMondayDate()
{
return GetMondayDate(DateTime.Now);
}
/// <summary>
/// 计算本周周日的日期
/// </summary>
/// <returns></returns>
public static DateTime GetSundayDate()
{
return GetSundayDate(DateTime.Now);
}
/// <summary>
/// 计算某日起始日期(礼拜一的日期)
/// </summary>
/// <param name="someDate">该周中任意一天</param>
/// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns>
public static DateTime GetMondayDate(DateTime someDate)
{
int i = someDate.DayOfWeek - DayOfWeek.Monday;
if (i == -1) i = 6;// i值 > = 0 因为枚举原因Sunday排在最前此时Sunday-Monday=-1必须+7=6。
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
return someDate.Subtract(ts);
}
/// <summary>
/// 计算某日结束日期(礼拜日的日期)
/// </summary>
/// <param name="someDate">该周中任意一天</param>
/// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns>
public static DateTime GetSundayDate(DateTime someDate)
{
int i = someDate.DayOfWeek - DayOfWeek.Sunday;
if (i != 0) i = 7 - i;// 因为枚举原因Sunday排在最前相减间隔要被7减。
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
return someDate.Add(ts);
}
}
}