Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am trying to parse my UploadDate as (dd/mm/yyyy) format on the client-side but currently its displaying date with time as well. I keep experiencing -- 'System.DateTime?' and 'string') --> error
with the following query below:
C#
public ActionResult Date(DateTime? start, DateTime? end)
        {
            DateTime yesterday = DateTime.Today.AddBusinessDays(-1);

            var query = from s in db.data_qy
                        select (s);

            var st = String.Format("{0:dd/MM/yyyy}", start);
            var en = String.Format("{0:dd/MM/yyyy}", end);

           // var db_start = DateTime.Parse(start).ToString("MM/dd/yyyy");

            //var datet1 = DateTime.ParseExact(start, "MM/dd/yyyy", CultureInfo.InvariantCulture);

            if (String.IsNullOrEmpty(st) && String.IsNullOrEmpty(en))
            {
                query = query.Where(c => c.UploadDate == yesterday);
            }
            else
            {
                if (!String.IsNullOrEmpty(st))
                {
                    query = query.Where(c => c.UploadDate >= st);// error line
                }

                if (!String.IsNullOrEmpty(en))
                {
                    query = query.Where(c => c.UploadDate <= en); // error line
                }
            }

            return View(query);
        }


I tried adding (? null) statement, but I am little unclear where I would add this.

Please note: That my 'UploadDate' fieldname is nullable datetime from my entity data model (database_qy).

Please advice, if possible. Thank you for your time and help.
Posted

C#
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());

// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));   
// The example displays the following output to the console: 
//       6/1/2008 7:47:00 AM 
//       6/1/2008 
//       6/1/2008 12:00 AM 
//       06/01/2008 00:00


The above code from here[^]
 
Share this answer
 
Yes you can't compare strings with DateTime objects (assuming UploadData is a DateTime), -> type confusion? maybe bettern not to overuse var keyword? At least I don't see you parsing that UploadData field. just the DateTimep parameters (without checking them for null).

So just add proper null checks for your nullable DateTimes,
and compare DateTime with DateTime (or strings with strings - but I see no need for that in your case)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900