【C#】单号生成器(编号规则、固定字符、流水号、产生业务单号)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129129787
【C#】日期范围生成器(构建开始日期、结束日期)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129040663
【C#】组件化开发,调用dll组件方法
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129492112
【C#】GridControl控件和List数据集双向绑定
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129423755
【C#】数据实体类使用
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128816638
【C#】单据审批流方案
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128972545
【C#】二维码标签制作及打印
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126884228
【C#】最全单据打印方案(设计打印模板、条形码&二维码、图片、字体)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129415723
【C#】条码管理操作手册
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126589496
【C#】WebAPI发布和IIS部署,及异常处理
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126539836
【C#】【提高编程效率】代码模板生成工具
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126890673
【C#】【提高编程效率】Excel数据导入工具
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126427323
【C#】Windows服务(Service)安装及启停方案
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124053794
【C#】穿透Session隔离,服务调用外部程序(无窗体界面解决)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124053033
【C#】任务计划实现,使用Quartz类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/123667723
【C#】《周计划管理关于产前准备模块》解决方案20200203
本文链接:https://blog.csdn.net/youcheng_ge/article/details/122919543
【C#】源码解析正则表达式
本文链接:https://blog.csdn.net/youcheng_ge/article/details/118337074
【C#】软件版本和文件MD5记录(XML操作)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/112513871
【C#】测试网络是否连通
本文链接:https://blog.csdn.net/youcheng_ge/article/details/110137288
【C#】程序开发,你使用DataTable还是List?
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129792726
【Inno Setup】安装包制作,项目打包方案
本文链接:https://blog.csdn.net/youcheng_ge/article/details/123665633
【Regex正则】最全正则表达式操作手册
本文链接:https://blog.csdn.net/youcheng_ge/article/details/108216964
我能抽象出整个世界,但是我不能抽象你。 想让你成为私有常量,这样外部函数就无法访问你。 又想让你成为全局常量,这样在我的整个生命周期都可以调用你。 可惜世上没有这样的常量,我也无法定义你,因为你在我心中是那么的具体。
哈喽大家好,本专栏为【项目实战】专栏,有别于【底层库】专栏,本专栏介绍项目开发过程中,遇到问题的解决方案,是我实际项目开发过程,相对成熟、可靠方法的提炼,我将这些问题的处理思路梳理,撰写本文分享给大家,大家遇到类似问题,可按本文方案处理。
本专栏会持续更新,不断完善,大家有任何问题,可以私信我。本专栏之间关联性较弱(文章之间依赖性较弱,没有阅读顺序区分)。如果您对本专栏感兴趣,欢迎关注吧,我将带你用最简洁的代码,实现最复杂的功能。
提示:本专栏为项目实战篇,未接触项目开发的同学可能理解困难,不推荐阅读。
【C#】日期范围生成(构建本周开始、结束日期)
我开发ERP系统,常遇到单据查询功能,业务员提出,他们并不需要显示全部数据,这样取数很慢,由于他们计划编排往往是一周内出的,所以一般只要查看一周的数据就好了。
故而,我们数据库存储的是固定日期,单据查询需要按每周的日期范围查询。有没有一种通用化的方法可以自动计算,周范围呢?
DateTimeExtension.cs日期扩展类,本类的最要用途:用户日期查询,可以设置“下拉框”查找项,通过本底层库,可以自动获取“开始日期”、“结束日期”。
可实现功能:本年、上年、本月、上月、本周、上周、今天、默认日、空日期、下周、下月、下年、昨天、财务月、财务上月、财务下月。
本底层库已经集成进Geyc.Utils.dll,可以直接下载 库组件,以下展示是本功能的源代码。
using System;
namespace WBF.Utils
{
public enum DatePeriod
{
ThisYear,
LastYear,
ThisMonth,
LastMonth,
ThisWeek,
LastWeek,
Custom,//自定义日期,初始化当天
None,//空日期
NextWeek,
NextMonth,
NextYear,
Yesterday,
Today,
AccountThisMonth,//财务月,需要先设置系统参数
AccountLastMonth,//财务上月,需要先设置系统参数
AccountNextMonth//财务下月,需要先设置系统参数
}
}
创建类StringExt.cs,用于字符串拼接,加引号,部分代码。
using System.Collections.Generic;
using System.Text;
namespace WBF.Utils
{
///
/// 字符串扩展类
/// 创建时间:2010-01-27
/// 说明:用于给字符串两边添加单引号、给字符串两边添加括号、使用And连接两个SQL条件、去掉末尾字符
/// 使用过程中发现错误,请联系作者修改 https://blog.csdn.net/youcheng_ge。
///
public static class StringExtension
{
#region 字符串连接
///
/// 给字符串两边添加单引号(同时会将内部单引号替换成双单引号)
///
///
///
public static string QuotedStr(this string S)
{
return "'" + S.Replace("'", "''") + "'";
}
///
/// 给字符串两边添加括号
///
/// 字符串
/// 返回带括号的字符串
public static string BracketStr(this string S)
{
return "(" + S + ")";
}
///
/// 字符串加指定字符包含
///
///
///
///
public static string QuotedWith(this string s, string a_quot)
{
return string.Concat(new string[]
{
"'",
a_quot,
s.Replace("'", "''"),
a_quot,
"'"
});
}
///
/// 使用And连接两个SQL条件
///
///
///
///
public static string ConcatSQL(this string S, string strCondition2)
{
if (S.Trim() == "" || strCondition2.Trim() == "")
{
return S + strCondition2;
}
return S + " and " + strCondition2;
}
#endregion
#region 字符串比较填充
///
/// 字符串比较
///
/// 被比较字符串
/// 比较字符串
///
public static bool SameText(this string s, string s2)
{
return string.Compare(s, s2, true) == 0;
}
///
/// 判断字符串是否为空
///
///
///
public static bool IsNullOrEmpty(this string s)
{
return string.IsNullOrEmpty(s);
}
///
/// 向右补充字符串长度
///
///
///
///
public static string PadRightCN(this string s, int a_intTotalWidth)
{
int num = 0;
checked
{
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
int num2 = (int)c;
if (num2 > 127)
{
num++;
}
}
return s.PadRight(a_intTotalWidth - num);
}
}
///
/// 向左补充字符串长度
///
///
///
///
public static string PadLeftCN(this string s, int a_intTotalWidth)
{
int num = 0;
checked
{
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
int num2 = (int)c;
if (num2 > 127)
{
num++;
}
}
return s.PadLeft(a_intTotalWidth - num);
}
}
#endregion
#region 字符串截取
///
/// 按指定间隔符,连接字符串
///
/// 数组
/// 间隔符
///
public static string BuildWith(this string[] array, string a_strSeparator)
{
string text = "";
string str = "";
for (int i = 0; i < array.Length; i++)
{
string str2 = array[i];
text = text + str + str2;
str = a_strSeparator;
}
return text;
}
///
/// 按指定间隔符,连接字符串
///
/// list列表
/// 间隔符
///
public static string BuildWith(this List<string> list, string a_strSeparator)
{
string text = "";
string str = "";
foreach (string current in list)
{
text = text + str + current;
str = a_strSeparator;
}
return text;
}
///
/// 字符串拆分成list
///
/// 源字段串
///
public static List<string> StringSplitToList(string a_strSource)
{
List<string> listField = new List<string>();
string[] array = a_strSource.Split(new char[]
{
',',
';'
});
foreach (string str in array)
{
if (str.Length > 0 && !listField.Contains(str))
{
listField.Add(str);
}
}
return listField;
}
///
/// 按指定分割符,字符串拆分成list
///
/// 源字段串
/// 分割字符
///
public static List<string> StringSplitToList(string a_strSource, char a_strSplit)
{
List<string> listField = new List<string>();
string[] array = a_strSource.Split(a_strSplit);
foreach (string str in array)
{
if (str.Length > 0 && !listField.Contains(str))
{
listField.Add(str);
}
}
return listField;
}
///
/// 截取中间字符串
///
/// 源字符串
/// 开始字符串
/// 末尾字符串
///
public static string SubMiddleString(string a_source, string a_strStart, string a_strEnd)
{
string l_ret = "";
int i = a_source.IndexOf(a_strStart);//空为0
if (i >= 0)
{
int j = a_source.IndexOf(a_strEnd, i + a_strStart.Length);
if (j > 0)
{
l_ret = a_source.Substring(i + a_strStart.Length, j - i - a_strStart.Length);
}
}
return l_ret;
}
#endregion
#region 删除字符串
///
/// 删除最后结尾的逗号
///
/// 源字符串
/// string
public static string DelLastComma(string a_strSource)
{
return a_strSource.Substring(0, a_strSource.LastIndexOf(","));
}
///
/// 删除最后结尾指定字符
///
/// 源字符串
/// 字符
/// string
public static string DelLastComma(string a_strSource, char a_char)
{
return a_strSource.Substring(0, a_strSource.LastIndexOf(a_char));
}
#endregion
#region 字符串重命名
///
/// 首字母大写
///
///
///
public static string ToUpperFirstword(string a_strValue)
{
if (string.IsNullOrEmpty(a_strValue))
{
return string.Empty;
}
else
{
return a_strValue.Substring(0, 1).ToUpper() + a_strValue.Substring(1).ToLower();
}
}
///
/// 驼峰命名法(第一个单字以小写字母+第二个单字大写首字母)
///
///
///
public static string ToCamelName(string a_strValue)
{
StringBuilder result = new StringBuilder();
if (string.IsNullOrEmpty(a_strValue))
{
return string.Empty;
}
else if (!a_strValue.Contains("_"))// 不含下划线,仅将首字母大写
{
return ToUpperFirstword(a_strValue);
}
else
{
// 用下划线将原始字符串分割
string[] camels = a_strValue.Split('_');
foreach (string camel in camels)
{
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (string.IsNullOrEmpty(camel))
{
continue;
}
if (result.Length == 0) // 第一个驼峰片段,全部字母都小写
{
result.Append(camel.ToLower());
}
else
{
// 其他的驼峰片段,首字母大写,其它小写
result.Append(camel.Substring(0, 1).ToUpper());
result.Append(camel.Substring(1).ToLower());
}
}
}
return result.ToString();
}
#endregion
}
}
using System;
namespace WBF.Utils
{
public class BizHandler
{
public static DateTime GetSettleMonthStartDate()
{
//return BizHandler.GetSettleMonthStartDate(SysParams.Instance.loginInfo.LoginTime);
return BizHandler.GetSettleMonthStartDate(DateTime.Now);
}
public static DateTime GetSettleMonthStartDate(DateTime a_dtDay)
{
int day = a_dtDay.Day;
//string value = SysParams.Instance.SysPar.GetValue("CW_AccoutIsNaturalMonth");
string value = "F";
if (string.Compare(value, "T", true) == 0)
{
return a_dtDay.Date.AddDays((double)(checked(0 - day + 1)));
}
//int num = (int)Convert.ToInt16(SysParams.Instance.SysPar.GetValue("month_start_day"));
int num = 0;
if (num == 0)
{
num = 1;
}
DateTime dateTime = new DateTime(a_dtDay.Year, a_dtDay.Month, 1);
DateTime d = dateTime.AddMonths(1);
DateTime d2 = dateTime.AddMonths(-1);
TimeSpan timeSpan = d - dateTime;
TimeSpan timeSpan2 = dateTime - d2;
DateTime dateTime2;
if ((double)num > timeSpan2.TotalDays)
{
dateTime2 = new DateTime(dateTime.Year, dateTime.Month, 1);
}
else if ((double)num > timeSpan.TotalDays)
{
dateTime2 = new DateTime(d2.Year, d2.Month, num);
}
else
{
dateTime2 = new DateTime(a_dtDay.Year, a_dtDay.Month, num);
}
if (dateTime2 > a_dtDay)
{
dateTime2 = dateTime2.AddMonths(-1);
}
return dateTime2;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WBF.Utils
{
public static class DateTimeExtension
{
public static void GetSearchDate(DatePeriod a_DatePeriod, out DateTime a_dtStartDate, out DateTime a_dtEndDate)
{
//DateTime dateTime = SysParams.Instance.loginInfo.LoginTime.Date;
DateTime dateTime = DateTime.Now.Date;
a_dtStartDate = dateTime;
a_dtEndDate = dateTime;
switch (a_DatePeriod)
{
case DatePeriod.ThisYear:
a_dtStartDate = new DateTime(dateTime.Year, 1, 1);
a_dtEndDate = new DateTime(dateTime.Year, 12, 31);
return;
case DatePeriod.LastYear:
dateTime = dateTime.AddYears(-1);
a_dtStartDate = new DateTime(dateTime.Year, 1, 1);
a_dtEndDate = new DateTime(dateTime.Year, 12, 31);
return;
case DatePeriod.ThisMonth:
a_dtStartDate = new DateTime(dateTime.Year, dateTime.Month, 1);
a_dtEndDate = a_dtStartDate.AddMonths(1).AddDays(-1.0);
return;
case DatePeriod.LastMonth:
dateTime = dateTime.AddMonths(-1);
a_dtStartDate = new DateTime(dateTime.Year, dateTime.Month, 1);
a_dtEndDate = a_dtStartDate.AddMonths(1).AddDays(-1.0);
return;
case DatePeriod.ThisWeek:
DateTimeExtension.GetWeekRange(dateTime, out a_dtStartDate, out a_dtEndDate);
return;
case DatePeriod.LastWeek:
dateTime = dateTime.AddDays(-7.0);
DateTimeExtension.GetWeekRange(dateTime, out a_dtStartDate, out a_dtEndDate);
return;
case DatePeriod.Custom:
break;
case DatePeriod.None:
a_dtStartDate = new DateTime(1900, 1, 1);
a_dtEndDate = new DateTime(1900, 1, 1);
return;
case DatePeriod.NextWeek:
dateTime = dateTime.AddDays(7.0);
DateTimeExtension.GetWeekRange(dateTime, out a_dtStartDate, out a_dtEndDate);
return;
case DatePeriod.NextMonth:
dateTime = dateTime.AddMonths(1);
a_dtStartDate = new DateTime(dateTime.Year, dateTime.Month, 1);
a_dtEndDate = a_dtStartDate.AddMonths(1).AddDays(-1.0);
return;
case DatePeriod.NextYear:
dateTime = dateTime.AddYears(1);
a_dtStartDate = new DateTime(dateTime.Year, 1, 1);
a_dtEndDate = new DateTime(dateTime.Year, 12, 31);
return;
case DatePeriod.Yesterday:
a_dtStartDate = dateTime.AddDays(-1.0);
a_dtEndDate = dateTime.AddDays(-1.0);
return;
case DatePeriod.Today:
a_dtStartDate = dateTime;
a_dtEndDate = dateTime;
return;
case DatePeriod.AccountThisMonth:
{
DateTime settleMonthStartDate = BizHandler.GetSettleMonthStartDate();
a_dtStartDate = settleMonthStartDate;
a_dtEndDate = settleMonthStartDate.AddMonths(1).AddDays(-1.0);
return;
}
case DatePeriod.AccountLastMonth:
{
DateTime settleMonthStartDate = BizHandler.GetSettleMonthStartDate();
a_dtStartDate = settleMonthStartDate.AddMonths(-1);
a_dtEndDate = settleMonthStartDate.AddDays(-1.0);
return;
}
case DatePeriod.AccountNextMonth:
{
DateTime settleMonthStartDate = BizHandler.GetSettleMonthStartDate();
a_dtStartDate = settleMonthStartDate.AddMonths(1);
a_dtEndDate = settleMonthStartDate.AddDays(-1.0).AddMonths(2);
break;
}
default:
return;
}
}
//public static DateTime GetServerDate()
//{
// FetchData fetchData = new FetchData();
// fetchData.InitSQL("select getdate() as d", false);
// if (fetchData.Fetch())
// {
// DataTable dataTable = fetchData.Data.Tables[0];
// return Convert.ToDateTime(dataTable.Rows[0][0]).Date;
// }
// throw new Abort();
//}
//public static DateTime GetServerTime()
//{
// FetchData fetchData = new FetchData();
// fetchData.InitSQL("select getdate() as d", false);
// if (fetchData.Fetch())
// {
// DataTable dataTable = fetchData.Data.Tables[0];
// return Convert.ToDateTime(dataTable.Rows[0][0]);
// }
// throw new Abort();
//}
public static void GetWeekRange(DateTime a_dTime, out DateTime a_dStartDay, out DateTime a_dEndDay)
{
checked
{
try
{
int num = (int)a_dTime.DayOfWeek;
num = ((num == 0) ? 7 : num);
a_dStartDay = a_dTime.AddDays((double)(0 - (num - 1))).Date;
a_dEndDay = a_dTime.AddDays((double)(7 - num)).Date;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
public static DateTime AddWeek(this DateTime a_Date, int a_WeekCount, ref int a_intYear, ref int a_intWeek)
{
DateTime dateTime = a_Date.AddDays((double)(checked(a_WeekCount * 7)));
DateTimeUtils.GetWeekIndex(dateTime, ref a_intYear, ref a_intWeek);
return dateTime;
}
public static DateTime AddWeek(this DateTime a_Date, int a_WeekCount, ref DateTime a_dStart, ref DateTime a_dEnd)
{
DateTime dateTime = a_Date.AddDays((double)(checked(a_WeekCount * 7)));
DateTimeUtils.GetWeekRange(dateTime, ref a_dStart, ref a_dEnd);
return dateTime;
}
//public static bool GetYearAndWeekIndex(DateTime a_dtDatetime, ref int a_intYear, ref int a_intWeek)
//{
// FetchData fetchData = new FetchData();
// string a_strSQL = string.Concat(new string[]
// {
// "select datepart(yyyy,",
// a_dtDatetime.ToShortDateString().QuotedStr(),
// ") as sys_year, datepart(wk,",
// a_dtDatetime.ToShortDateString().QuotedStr(),
// ") as sys_week"
// });
// fetchData.InitSQL(a_strSQL, false);
// if (fetchData.Fetch())
// {
// a_intYear = (int)Convert.ToInt16(fetchData.Data.Tables[0].Rows[0]["sys_year"]);
// a_intWeek = (int)Convert.ToInt16(fetchData.Data.Tables[0].Rows[0]["sys_week"]);
// return true;
// }
// a_intYear = 0;
// a_intWeek = 0;
// return false;
//}
}
}
using System;
namespace WBF.Utils
{
public class DateTimeUtils
{
public static string GetWeekDay(string strDate)
{
string weekDay;
try
{
DateTime a_dtDateTime = Convert.ToDateTime(strDate);
weekDay = DateTimeUtils.GetWeekDay(a_dtDateTime);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return weekDay;
}
public static string GetWeekDay(DateTime a_dtDateTime)
{
string weekDay;
try
{
int dayOfWeek = (int)a_dtDateTime.DayOfWeek;
weekDay = DateTimeUtils.GetWeekDay(dayOfWeek);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return weekDay;
}
public static int GetMaxWeekOfYear(int a_intYear)
{
int result;
try
{
DateTime minValue = DateTime.MinValue;
DateTime minValue2 = DateTime.MinValue;
DateTimeUtils.GetStartAndEndDate(a_intYear, ref minValue, ref minValue2);
result = checked(minValue2.Subtract(minValue).Days + 1) / 7;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return result;
}
public static int GetMaxWeekOfYear(DateTime dTime)
{
int maxWeekOfYear;
try
{
maxWeekOfYear = DateTimeUtils.GetMaxWeekOfYear(dTime.Year);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return maxWeekOfYear;
}
public static void GetWeekIndex(DateTime dTime, ref int a_intYear, ref int a_intWeek)
{
checked
{
try
{
DateTime minValue = DateTime.MinValue;
DateTime minValue2 = DateTime.MinValue;
DateTimeUtils.GetStartAndEndDate(dTime.Year, ref minValue, ref minValue2);
if (dTime > minValue2)
{
a_intYear = dTime.Year + 1;
a_intWeek = 1;
}
else if (dTime < minValue)
{
a_intYear = dTime.Year - 1;
a_intWeek = DateTimeUtils.GetMaxWeekOfYear(a_intYear);
}
else
{
int num = (int)dTime.DayOfWeek;
num = ((num == 0) ? 7 : num);
DateTime dateTime = dTime.AddDays((double)(0 - (num - 1)));
a_intYear = dTime.Year;
a_intWeek = dateTime.Subtract(minValue).Days / 7 + 1;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
private static void GetStartAndEndDate(int a_intYear, ref DateTime a_dtStart, ref DateTime a_dtEnd)
{
DateTime dateTime = new DateTime(a_intYear, 1, 1);
DateTime dateTime2 = new DateTime(a_intYear, 12, 31);
int num = (int)dateTime.Date.DayOfWeek;
int num2 = (int)dateTime2.Date.DayOfWeek;
if (num == 0)
{
num = 7;
}
if (num2 == 0)
{
num2 = 7;
}
checked
{
if (num > 4)
{
a_dtStart = dateTime.AddDays((double)(7 - num + 1));
}
else
{
a_dtStart = dateTime.AddDays((double)(0 - (num - 1)));
}
if (num2 < 4)
{
a_dtEnd = dateTime2.AddDays((double)(0 - num2));
return;
}
a_dtEnd = dateTime2.AddDays((double)(7 - num2));
}
}
public static void GetWeekIndex(string a_strDate, ref int a_intYear, ref int a_intWeek)
{
try
{
DateTime dTime = Convert.ToDateTime(a_strDate);
DateTimeUtils.GetWeekIndex(dTime, ref a_intYear, ref a_intWeek);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static string GetWeekRange(DateTime dTime, ref DateTime a_dStartDay, ref DateTime a_dEndDay)
{
checked
{
string result;
try
{
int num = (int)dTime.DayOfWeek;
num = ((num == 0) ? 7 : num);
a_dStartDay = dTime.AddDays((double)(0 - (num - 1))).Date;
a_dEndDay = dTime.AddDays((double)(7 - num)).Date;
result = DateTimeUtils.WeekRangeToString(a_dStartDay, a_dEndDay);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return result;
}
}
public static string GetWeekRange(int a_intYear, int a_intWeekIndex, ref DateTime a_dtStartDay, ref DateTime a_dtEndDay)
{
string result;
try
{
if (a_intWeekIndex < 1)
{
throw new Exception("周数必须大于0!");
}
DateTime minValue = DateTime.MinValue;
DateTime minValue2 = DateTime.MinValue;
DateTimeUtils.GetStartAndEndDate(a_intYear, ref minValue, ref minValue2);
int maxWeekOfYear = DateTimeUtils.GetMaxWeekOfYear(a_intYear);
if (a_intWeekIndex > maxWeekOfYear)
{
throw new Exception(a_intYear.ToString() + "年没有第" + a_intWeekIndex.ToString() + "周");
}
a_dtStartDay = minValue.AddDays((double)(checked((a_intWeekIndex - 1) * 7))).Date;
a_dtEndDay = a_dtStartDay.AddDays(6.0).Date;
result = DateTimeUtils.WeekRangeToString(a_dtStartDay, a_dtEndDay);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return result;
}
private static string WeekRangeToString(DateTime weekRangeStart, DateTime weekRangeEnd)
{
string str = weekRangeStart.ToString("yyyy/MM/dd");
string str2 = weekRangeEnd.ToString("yyyy/MM/dd");
return str + "~" + str2;
}
public static string GetWeekDay(int index)
{
string result = string.Empty;
switch (index)
{
case 0:
result = "星期日";
break;
case 1:
result = "星期一";
break;
case 2:
result = "星期二";
break;
case 3:
result = "星期三";
break;
case 4:
result = "星期四";
break;
case 5:
result = "星期五";
break;
case 6:
result = "星期六";
break;
}
return result;
}
}
}
写了一个测试用例,DateTimeExtension.GetSearchDate(参数1, out 参数2, out 参数3);
参数1:日期类型(去年、今年、明年、上月、本月、下月、上周、本周、下周、昨天、今天、明天)。
参数2:开始日期,回调参数。
参数3:结束日期,回调参数。
我们看一下调试效果,放在Main()主函数执行。
static void Main(string[] args)
{
DateTime l_dtstart;
DateTime l_dtend;
DateTimeExtension.GetSearchDate(DatePeriod.Today, out l_dtstart, out l_dtend);
DateTimeExtension.GetSearchDate(DatePeriod.LastWeek, out l_dtstart, out l_dtend);
DateTimeExtension.GetSearchDate(DatePeriod.ThisWeek, out l_dtstart, out l_dtend);
DateTimeExtension.GetSearchDate(DatePeriod.Yesterday, out l_dtstart, out l_dtend);
}
我用于MES系统,题头的查找项“开始日期”、“结束日期”,方便业务员按范围查找数据,界面设计样式如下:
①“日期类型”选择“上月”,自动算出“上个月”开始日期、结束日期的范围,支持区分 大小月、闰月。
②“日期类型”选择“本月”,自动算出“上个月”开始日期、结束日期的范围,支持区分 大小月、闰月。
③“日期类型”选择“上周”,自动算出“上周”开始日期、结束日期的范围。
④“日期类型”选择“本周”,自动算出“上周”开始日期、结束日期的范围。
项目源码如下,可以直接复制。
//设置默认时间选择
void SetDefaultDate()
{
baseDataInput_开始日期.ReadOnly = true;
baseDataInput_结束日期.ReadOnly = true;
baseDataInput_日期类型.SelectIndex(1);
SetSearchDate(DatePeriod.ThisMonth);
}
private void baseDataInput_日期类型_EditValueChanged(object sender, EventArgs e)
{
if (baseDataInput_日期类型.StringValue == "上月")
{
baseDataInput_开始日期.ReadOnly = true;
baseDataInput_结束日期.ReadOnly = true;
SetSearchDate(DatePeriod.LastMonth);
}
else if (baseDataInput_日期类型.StringValue == "本月")
{
baseDataInput_开始日期.ReadOnly = true;
baseDataInput_结束日期.ReadOnly = true;
SetSearchDate(DatePeriod.ThisMonth);
}
else if (baseDataInput_日期类型.StringValue == "上周")
{
baseDataInput_开始日期.ReadOnly = true;
baseDataInput_结束日期.ReadOnly = true;
SetSearchDate(DatePeriod.LastWeek);
}
else if (baseDataInput_日期类型.StringValue == "本周")
{
baseDataInput_开始日期.ReadOnly = true;
baseDataInput_结束日期.ReadOnly = true;
SetSearchDate(DatePeriod.ThisWeek);
}
else if (baseDataInput_日期类型.StringValue == "当日")
{
baseDataInput_开始日期.ReadOnly = true;
baseDataInput_结束日期.ReadOnly = true;
SetSearchDate(DatePeriod.Today);
}
else
{
baseDataInput_开始日期.ReadOnly = false;
baseDataInput_结束日期.ReadOnly = false;
SetSearchDate(DatePeriod.Today);
}
}
//初始化时间下拉控件
void LCBDateInit()
{
baseDataInput_日期类型.AddItem("上月");
baseDataInput_日期类型.AddItem("本月");
baseDataInput_日期类型.AddItem("上周");
baseDataInput_日期类型.AddItem("本周");
baseDataInput_日期类型.AddItem("当日");
baseDataInput_日期类型.AddItem("自定义");
}
//时间设置
public void SetSearchDate(DatePeriod a_dpDate)
{
DateTime l_dtstart;
DateTime l_dtend;
DateTimeExtension.GetSearchDate(a_dpDate, out l_dtstart, out l_dtend);
baseDataInput_开始日期.DateTimeValue = l_dtstart;
baseDataInput_结束日期.DateTimeValue = l_dtend;
}