简体   繁体   中英

how to get only date part from date string in C#

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));

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  ));

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'

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");

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 );

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM