DateTime format to SQL format using C#

前端 未结 15 1200
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 02:47

I am trying to save the current date time format from C# and convert it to an SQL Server date format like so yyyy-MM-dd HH:mm:ss so I can use it for my UP

15条回答
  •  抹茶落季
    2020-11-28 03:02

    Let's use the built in SqlDateTime class

    new SqlDateTime(DateTime.Now).ToSqlString()
    

    But still need to check for null values. This will throw overflow exception

    new SqlDateTime(DateTime.MinValue).ToSqlString()
    

    SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

提交回复
热议问题