0

I would like to change the DateTime format from asp.net format to SQL Datetime using if statement. How can I do this ?

 public DateTime AvailableSince
{
    get
    {
        return _availableSince;
    }
    set
    {
        _availableSince = value;
    }
}
Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
  • 1
    Does this answer your question? [DateTime format to SQL format using C#](https://stackoverflow.com/questions/17418258/datetime-format-to-sql-format-using-c-sharp) – Corentin Pane Nov 07 '19 at 14:22
  • 1
    Could you give more details about what you mean by "SQL Datetime"? You can use `DateTime` values directly as parameters in `SqlCommand`, which is almost always the right way to go. (If you're talking about converting to a specific text format, I'd strongly advise against using that *when talking to the database*.) – Jon Skeet Nov 07 '19 at 16:25
  • Neither .Net not SQL (any flavor) store DateTime in a "format", both use some binary value. – Hans Kesting Nov 07 '19 at 17:58

1 Answers1

0

Try this:

DateTime myDateTime = DateTime.Now;
string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
  • I'm not convinced this is a good idea anyway for what the OP wants to do, but it's definitely worth specifying the invariant culture. Using this in a culture with a non-Gregorian default calendar will give results which would surprise most developers. – Jon Skeet Nov 07 '19 at 16:30