DateTime format to SQL format using C#

前端 未结 15 1006
伪装坚强ぢ
伪装坚强ぢ 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:18

    if you want to store current date in table so you can use

    GETDATE();

    or pass this function as a parameter

    eg. 'update tblname set curdate=GETDATE() where colname=123'

    0 讨论(0)
  • 2020-11-28 03:20

    Why not skip the string altogether :

    SqlDateTime myDateTime = DateTime.Now;
    
    0 讨论(0)
  • 2020-11-28 03:22

    Using the standard datetime format "s" will also ensure internationalization compatibility (MM/dd versus dd/MM):

    myDateTime.ToString("s");
    
    => 2013-12-31T00:00:00
    

    Complete Options: (code: sample result)

    d: 6/15/2008 
    D: Sunday, June 15, 2008 
    f: Sunday, June 15, 2008 9:15 PM 
    F: Sunday, June 15, 2008 9:15:07 PM 
    g: 6/15/2008 9:15 PM 
    G: 6/15/2008 9:15:07 PM 
    m: June 15 
    o: 2008-06-15T21:15:07.0000000 
    R: Sun, 15 Jun 2008 21:15:07 GMT 
    s: 2008-06-15T21:15:07 
    t: 9:15 PM 
    T: 9:15:07 PM 
    u: 2008-06-15 21:15:07Z 
    U: Monday, June 16, 2008 4:15:07 AM 
    y: June, 2008 
    
    'h:mm:ss.ff t': 9:15:07.00 P 
    'd MMM yyyy': 15 Jun 2008 
    'HH:mm:ss.f': 21:15:07.0 
    'dd MMM HH:mm:ss': 15 Jun 21:15:07 
    '\Mon\t\h\: M': Month: 6 
    'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
    

    Supported in .NET Framework: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
    Reference: DateTime.ToString Method

    0 讨论(0)
提交回复
热议问题