System.datetime到字符串错误 [英] System.datetime to string error

查看:71
本文介绍了System.datetime到字符串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我试图在客户端解析我的UploadDate为(dd / mm / yyyy)格式,但目前显示日期与时间同样。我一直在体验 - 'System.DateTime?'和'string') - >错误

以下查询:

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:

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





我尝试添加(?null)语句,但我不清楚在哪里添加它。



请注意:我的'UploadDate'字段名是来自我的实体数据模型(database_qy)的可以为空的日期时间。



如果可能,请提供建议。感谢您的时间和帮助。



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.

推荐答案

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





以上代码来自此处 [ ^ ]


是的你无法将字符串与DateTime对象进行比较(假设UploadData是DateTime), - >型混乱?也许不要过度使用var关键字?至少我没有看到你解析那个UploadData字段。只是DateTimep参数(不检查它们是否为null)。



所以只需为你的可以为空的DateTimes添加适当的空值检查,

并将DateTime与DateTime(或带字符串的字符串)进行比较 - 但我看不到在你的情况下需要)
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)


这篇关于System.datetime到字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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