如何用#计算asp.net中的日期 [英] how to calculate date in asp.net with #

查看:80
本文介绍了如何用#计算asp.net中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据列表中显示最近三个月的数据;
我在表日期中有一个列是日期时间类型.
如果用户想查看最近三个月的数据,那么可以,因此如何计算数据

i want to show last three month data in datalist;
i hv one column in table date which is datetime type.
if user want to see last three month data then they can,so how to calculate data

推荐答案

类似

Something like

List<datetime> dates = new List<datetime>();

DateTime userDate = DateTime.Now;

DateTime currentDate = userDate.AddMonths(-3);

while (currentDate <= userDate)
{
    dates.Add(currentDate);
    currentDate = currentDate.AddDays(1);
}

DataList dl = new DataList();
dl.DataSource = dates;
dl.DataBind();
</datetime></datetime>


您需要为查询创建日期范围.查询本身的外观完全取决于您的数据源. (即sql,excel文件,文本文件???).如果您的用户选择了一个日期,而您想计算3个月前的日期,则只需执行以下操作:
You need to create a date range for your query. What the query itself looks like is entirely dependent on what your data source is going to be. (i.e. sql, excel file, text file???). If your user picks a date and you want to calculate the date 3 months previous, then you just need to do the following:
DateTime startDate = userDate.AddMonths( -3 );


SELECT * FROM table WHERE DATEDIFF(mm,date column,getdate())<=3


这篇关于如何用#计算asp.net中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆