0

I'm trying to storage into my database ONLY the DATE not DATE AND TIME, only Date, but it always came with the time together.

This is how I do to storage only the date inside of String :

string.Format("{0:dd/MM/yyyy}", DateTime.Now)

And works fine, but now, I want to storage a StringDate to a Date in my SQL Server database. So I did this :

EntityTable...

 [Column(TypeName = "date")]
 public DateTime? InicialDate { get; set; }

Controller

InicialDate = DateTime.ParseExact("01-12-2016",
                             "dd/MM/yyyy",
                             new CultureInfo("pt-BR"));

And inside of my Table, the Date is not at the right format as I expected, I came like this :

2017-12-01 and not, DAY-MONTH-YEAR....

And if I try to make a query and retrieve the date, i came like this :

"InicialDate": "2017-12-01T00:00:00"

I just want to save ONLY the DATE without time!

4 Answers4

0

Correct way is creating your Date type for using whole project. Also there is some Data class already developed: https://github.com/claycephas/csharp-date

mkysoft
  • 5,392
  • 1
  • 21
  • 30
0

If you've already created the table with [Column(TypeName = "date")], please check the schema and data of the table, the type of column InicialDate should be date, which doesn't save the time portion of datetime.

The time portion you saw in the result (i.e. T00:00:00) was appended by EF, when it was cast to DateTime structure.

Vej
  • 390
  • 1
  • 7
  • 28
  • got it, so how to remove this `T00:00:00` ? –  Mar 25 '17 at 19:30
  • Use [custom format](https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) on the field, e.g. `InicialDate.ToString("yyyy-MM-dd")` – Vej Mar 25 '17 at 19:37
  • You can also refer [this answer](http://stackoverflow.com/questions/18635599/specifying-a-custom-datetime-format-when-serializing-with-json-net), if the final result is generated by Json.NET from Newtonsoft. – Vej Mar 25 '17 at 19:40
0

DateTime is internally just a single number (probably an int/long, representing the number of seconds since the Epoch; not sure about this, though; but it doesn't really matter here) and how it's displayed as a string depends on the format used. DateTime.ParseExact("01-12-2016", "dd/MM/yyyy", new CultureInfo("pt-BR")); parses the date into DateTime internal representation according to the pt-BR rules for displaying date, then when EF is posting this to the db it converts the internal representation to ISO-8601 textual form (because SQL is text-based), then the db engine again converts it into a number form and stores it. When you look in the db, you see it as 2017-12-01, because the tool you use to do this is set up to display dates in the ISO-8601 format.

So, the date is stored correctly, what you seed depends on the format the particular app uses to display dates.

Jakub Hromadík
  • 506
  • 2
  • 4
  • 11
-2

Change DateTime to nvarchar(10) in database if u use DateTime in database 00.00.00 will be automatically assigned by database. So, you are getting time with it while retrieving.

Change column datatype to Varchar issue will be resolved.

Sincerely,

Thiyagu Rajendran

**Please mark the replies as answers if they help and unmark if they don't.