方法“ToString”无过载投放日期时需要1个参数 [英] No overload for method 'ToString" takes 1 arguments when casting date

查看:179
本文介绍了方法“ToString”无过载投放日期时需要1个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的Angular ui-Datepicker的日期保存到我的SQL数据库。日期格式为(10-27-2015 12:00 AM),但不会保存。我尝试使用以下命令将其转换为SQL DateTime格式:

I am trying to save a date from my Angular ui-Datepicker to my SQL database. The date is in the format (10-27-2015 12:00 AM) but it will not save. I tried using the following to convert it to SQL DateTime format:

    DateTime? myDate = form.dteStartDate;
    string sqlFormattedDate = myDate.ToString("yyyy-MM-dd HH:mm:ss");

但是我收到错误无重载方法ToString'需要1个参数SQL中的字段是'datetime'。

But I receive the error "No overload for method 'ToString' takes 1 arguments. The field in SQL is type 'datetime'.

非常感谢任何帮助。

推荐答案

p>您要使用 DateTime.ToString(format) 不是 Nullable< DateTime> .ToString(无重载)

You want to use DateTime.ToString(format) not Nullable<DateTime>.ToString(no overload):

DateTime? myDate = form.dteStartDate;
string sqlFormattedDate = myDate.Value.ToString("yyyy-MM-dd HH:mm:ss");

当然这并不处理没有价值的情况,也许是这样的:

Of course this doesn't handle the case that there is no value. Perhaps something like this:

string sqlFormattedDate = myDate.HasValue 
    ? myDate.Value.ToString("yyyy-MM-dd HH:mm:ss")
    : "<not available>";

这篇关于方法“ToString”无过载投放日期时需要1个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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