如何在asp.net c#中获取当前日,周,月 [英] How can I get the current day,week,month in asp.net c#

查看:112
本文介绍了如何在asp.net c#中获取当前日,周,月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DateTime.Now();在我的程序中保存事务并保存这样的2/21/2014 7:21:56 PM。我希望在一天内完成所有交易。如何在2014年2月21日内完成所有交易。





  string  strQuery =  选择Item_Purchased_Number,ProductID,ProductName,quantity,Branch ,Date_Purchased from Customer_Purchased Where Branch ='Manila' +  'AND Date_Purchased =' + DateTime.Now.AddHours(-24).ToString(); 





这是我的查询。请帮我。非常需要。谢谢。 :)

解决方案

好的,首先要注意的是DateTime.Now是一个非常具体的时间:它实际上就是您访问该属性的时刻。当你从那里减去24小时后,你得到了相同的确切时间,但昨天。由于此时间精确到毫秒或更短,因此您的查询非常非常不可能工作,除非您很幸运并且每天都在同一时间发布帖子!

所以,开始通过查看 DateTime.Now.Date [ ^ ]丢弃时间部分并返回午夜。

然后更改查询以查看大于该值的值:

  string  strQuery =  选择Item_Purchased_Number,ProductID,ProductName, Quantity,Branch,Date_Parchased from Customer_Purchased Where Branch ='Manila' +  'AND Date_Purchased> = ' + DateTime.Now.Date.ToString(); 

(或b etter仍然,使用参数化查询并且不转换为字符串 - 这可能会导致某些系统出现问题,因为根据PC系统设置,字符串格式可能会有很大差异。



如果你想要特定的日期结果,那么我的方式就是使用SQL DATEDIFF函数:

 ... WHERE DATEDIFF(d, Date_Purchased,  + DateTime.Now.ToString()+)=  0  

今天会发现,= 1将是昨天,依此类推。


请尝试如下(SQL注入 -



注意:
为简单起见,我只提取了日期部分。所以用你的其他where子句验证完成它。



  var  sqlCommand =  new  SqlCommand(); 
sqlCommand.CommandText = Item_Purchased_Number,ProductID,ProductName,quantity,Branch,Date_Purchased from Customer_Purchased,其中Date_Purchased> = @DatePurchased;
sqlCommand.Parameters.AddWithValue( @ DatePurchased,DateTime.Today);


Im using DateTime.Now(); in saving transaction in my program and its saves like this "2/21/2014 7:21:56 PM". I want to get all the transaction within the day. How can I get all the transaction within "2/21/2014".


string strQuery = "Select Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased Where Branch = 'Manila'" + "'AND Date_Purchased = '" + DateTime.Now.AddHours(-24).ToString() ;



This is my query. Please help me. Badly needed. Advance thanks. :)

解决方案

OK, the first thing to notice is that DateTime.Now is a very specific time: it is literally the moment in time when you access the property. And when you subtract 24 hours from that, you get the same exact time, but yesterday. Since this time is accurate to milliseconds or less, it is very, very unlikely that your query will ever work, unless you are lucky and posts are made at the exact same time each day!
So, start by looking at DateTime.Now.Date[^] which discards the time part and goes back to midnight.
Then change your query to look at values greater than that:

string strQuery = "Select Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased Where Branch = 'Manila'" + "'AND Date_Purchased >= '" + DateTime.Now.Date.ToString();

(or better still, use a parameterised query and don't convert to string - this can cause problems in some systems, since the string format can be very different depending on the PC system settings).

If you want specific days results, then the way I do it is to use the SQL DATEDIFF function:

...WHERE DATEDIFF(d, Date_Purchased, " + DateTime.Now.ToString() + ")=0

Will find today, "=1" will be yesterday, and so forth.


Please try is as below (SQL injection-proof).

Note:
For simplicity,I extracted date part only.So complete it with your other where clause validations.

var sqlCommand = new SqlCommand();
sqlCommand.CommandText = "Item_Purchased_Number,ProductID,ProductName,quantity,Branch, Date_Purchased from Customer_Purchased where Date_Purchased >= @DatePurchased";
sqlCommand.Parameters.AddWithValue("@DatePurchased", DateTime.Today);


这篇关于如何在asp.net c#中获取当前日,周,月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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