我在网上找了半天,没有找到,如何获得默认时间,也就是0001-1-1-1 0:00:00 恩 应该就是这个时间,
还有 怎样比较两个时间是否相等呢?
我想做一个treeview,有节点新增客户,其中有子节点本日新增,本周新增,本月新增,通过系统当前时间来获得本日,本周,本月的时间范围,通过本日新增,本周新增,本月新增来实现导航,比如单击本月新增,就将通过系统当前时间获得到的本月的时间范围作为条件,在数据库里面进行搜索,请各位帮帮啦
总结:
1,如何获得默认的时间值
2,如何比较两个时间是否相等
3,如何通过系统的当前时间获得本日,本周,本月的时间范围
不知道我想的这个逻辑是否合理,我已经实现了其他的一些东西,在这里希望各位帮忙了,完成这个treeview!!!
还有 怎样比较两个时间是否相等呢?
我想做一个treeview,有节点新增客户,其中有子节点本日新增,本周新增,本月新增,通过系统当前时间来获得本日,本周,本月的时间范围,通过本日新增,本周新增,本月新增来实现导航,比如单击本月新增,就将通过系统当前时间获得到的本月的时间范围作为条件,在数据库里面进行搜索,请各位帮帮啦
总结:
1,如何获得默认的时间值
2,如何比较两个时间是否相等
3,如何通过系统的当前时间获得本日,本周,本月的时间范围
不知道我想的这个逻辑是否合理,我已经实现了其他的一些东西,在这里希望各位帮忙了,完成这个treeview!!!
DateTimevar1=newDateTime(0);2,如何比较两个时间是否相等 DateTime var1 = new DateTime(0);
DateTime var2 = DateTime.Now; if (var1.Equals(var2))
{
MessageBox.Show("相等");
}3,如何通过系统的当前时间获得本日,本周,本月的时间范围 DateTime.Now.Year; //今天所在的年
DateTime.Now.Month; //今天所在的月
DateTime.Now.Day; //今天的日期
DateTime.Now.DayOfWeek; //今天是一周中的那一天
int i = (int)DateTime.Now.DayOfWeek;
DateTime sd = DateTime.Now.AddDays(0 - i);
DateTime ed = DateTime.Now.AddDays(6 - i);
select * from tb where addDate>= convert(varchar(7), Getdate(),120) + '-01'
and adddate< convert(varchar(7),dateadd(month,1,Getdate()),120) + '-01'
测试数据
create table users
(
Id int identity(1,1) primary key,
UserName nvarchar(20) null,
CreateTime datetime default(1900-01-01)
)insert into users (UserName,CreateTime)
select '张三',getdate()
union
select '李四','2009-05-06'
union
select '王五', '2009-05-01'
前端处理 private const string conString = "Server=SZSOFT-SZZB\\SQLEXPRESS;database=office;uid=sa;pwd=abc_!123456;"; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BuildUserTree();
}
} /// <summary>
/// 生成树
/// </summary>
private void BuildUserTree()
{
trvUser.Nodes.Clear(); TreeNode root = new TreeNode("新增用户");
CreateSubNode(root);
trvUser.Nodes.Add(root); } /// <summary>
/// 加载子节点
/// </summary>
/// <param name="parentNode"></param>
private void CreateSubNode(TreeNode parentNode)
{ TreeNode subNode = null;
DataTable dt = GetAllUsers(); DateTime now=DateTime.Now;
int dow=(int)now.DayOfWeek;
//星期天为第一天
int di=-dow;
DateTime startWeek=now.AddDays(di);
int ei = 6 - dow ;
DateTime endWeek = now.AddDays(ei); DateTime startMonth = Convert.ToDateTime(string.Format("{0}-{1}-01",now.Year,now.Month));
DateTime endMonth = startMonth.AddMonths(1).AddDays(-1); // 今天新增用户
DataRow[] drFind = dt.Select(string.Format("CreateTime='{0}'", now.ToString("yyyy-MM-dd")));
TreeNode todayNode = new TreeNode("今天新增");
foreach(DataRow dr in drFind)
{
subNode = new TreeNode(dr["UserName"].ToString());
todayNode.ChildNodes.Add(subNode);
}
parentNode.ChildNodes.Add(todayNode); // 本周新增用户
drFind = dt.Select(string.Format("CreateTime>='{0}' and CreateTime <='{1}' ", startWeek.ToString("yyyy-MM-dd"),endWeek.ToString("yyyy-MM-dd")));
TreeNode weekNode = new TreeNode("本周新增");
foreach(DataRow dr in drFind)
{
subNode = new TreeNode(dr["UserName"].ToString());
weekNode.ChildNodes.Add(subNode);
}
parentNode.ChildNodes.Add(weekNode); // 月新增用户
drFind = dt.Select(string.Format("CreateTime>='{0}' and CreateTime <='{1}' ", startMonth.ToString("yyyy-MM-dd"), endMonth.ToString("yyyy-MM-dd")));
TreeNode monthNode = new TreeNode("本月新增");
foreach (DataRow dr in drFind)
{
subNode = new TreeNode(dr["UserName"].ToString());
monthNode.ChildNodes.Add(subNode);
}
parentNode.ChildNodes.Add(monthNode);
} /// <summary>
/// 获取数据
/// </summary>
/// <returns></returns>
private DataTable GetAllUsers()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = conString;
con.Open();
string sqlSelect = "select Id,UserName,convert(varchar(10), CreateTime,120) as CreateTime From Users";
SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}