Reputation: 267
i need to get in this format "2015-10-03" , but im getting like this "10/3/2015" and "10/3/2015 12:00:00 AM" both are not working in my query .because my updateddate datatype is date only
Fromdate = Txtbox_AjaxCalFrom.Text.Trim();// 10/3/2015
DateTime frmdt = Convert.ToDateTime(Fromdate);// 10/3/2015 12:00:00 AM
ToDate = Txtbox_AjaxCalTo.Text.Trim();
DateTime todt = Convert.ToDateTime(Fromdate);
i want query like this
updateddate between '2015-10-03' and '2015-10-03'
full query
gvOrders.DataSource = GetData(string.Format("select * from GoalsRoadMap where Activities='{0}' and project ='" + ProjectName + "' and updateddate between '2015-10-03' and '2015-10-03' ", customerId));
Upvotes: 0
Views: 9356
Reputation: 16086
None of the above answer worked for me. Sharing what worked:
string Text="22/11/2009";
DateTime date = DateTime.ParseExact(Text, "dd/MM/yyyy", null);
Console.WriteLine("update date => "+date.ToString("yyyy-MM-dd"));
Upvotes: 0
Reputation: 1071
If your SQL date items stored as date and time, not just date, then your problem will be that for items between '2015-10-03' and '2015-10-03', it returns nothing.
So you just need to add one day to your toDate.Date
that its Time
is 12:00:00 AM.
Something like this:
Fromdate = Txtbox_AjaxCalFrom.Text.Trim();// 10/3/2015
string frmdt = Convert.ToDateTime(Fromdate).Date; // 10/3/2015 12:00:00 AM
//Or
//var frmdt = DateTime.Parse(Txtbox_AjaxCalFrom.Text).Date;
ToDate = Txtbox_AjaxCalTo.Text.Trim();
string todt = Convert.ToDateTime(todt).Date.AddDays(1);// 11/3/2015 12:00:00 AM
now if run your query like this, it may works:
var items = allYourSearchItems.Where(x => x.Date >= frmdt && x.Date < todt );
Upvotes: 0
Reputation: 154
try this:
DateTime frmdt = Convert.ToDateTime(fromDate);
string frmdtString = frmdt.ToString("yyyy-MM-dd");
or at once:
string frmdt = Convert.ToDateTime(fromDate).ToString("yyyy-MM-dd");
So your code could look like this:
Fromdate = Txtbox_AjaxCalFrom.Text.Trim();// 10/3/2015
string frmdt = Convert.ToDateTime(Fromdate).ToString("yyyy-MM-dd");
ToDate = Txtbox_AjaxCalTo.Text.Trim();
string todt = Convert.ToDateTime(todt).ToString("yyyy-MM-dd");
gvOrders.DataSource = GetData(string.Format("select * from GoalsRoadMap where Activities='{0}' and project ='" + ProjectName + "' and updateddate between '{1}' and '{2}' ", customerId, frmdt, todt ));
Upvotes: 3
Reputation: 1011
As mentioned by Christos you can format DateTime to universal Date string (here are examples https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx). But right way to create queries is to use parameters. The following sample for SqlClient, but the idea is the same for other providers.
var cmd=new SqlCommand("UPDATE Table1 SET field1=@value WHERE dateField=@date");
cmd.Parameters.Add("@date",SqlDbType.Date).Value=myDateTime; //myDateTime is type of DateTime
...
And you have an error in SQL, BETWEEN '2015-10-03' AND '2015-10-03' will return nothing. Instead try this one dateField>='2015-10-03' AND dateField<='2015-10-03'
Upvotes: 2
Reputation: 53958
You could format your date string as you wish. Let that dt
is your DateTime
object with the value 10/3/2015 12:00:00 AM. Then you can get the string representation you want like below:
var formatted = dt.ToString("yyyy-MM-dd");
Upvotes: 1