-1

Is there any possible way for saving DateTime.Now to ddMMyyyy format without using ToString(). Because whenever I use the string operation the statement is not accepted by entity framework. I need to add DateTime to DB in date format of ddMMyyyy. Is there any way??

Harshal Bhamare
  • 352
  • 1
  • 6
  • 16
abdulkhadar7
  • 105
  • 1
  • 1
  • 11
  • this is probably a misunderstanding. the display format and the database storage format of a datetime are independent from each other. could you expand on "...not accepted by the framework...", and show us your code? – Cee McSharpface Nov 15 '16 at 13:19
  • 2
    Dont store datetimes as strings – Tim Schmelter Nov 15 '16 at 13:19
  • 2
    "In date format" *means* as a string. Either you want a string, or you don't. If you don't want a string, there's no format - you've just got a date/time value. If you want a string, you should use `ToString`. I'd strongly suggest *not* storing it as a string. – Jon Skeet Nov 15 '16 at 13:19
  • The problem is not in your C# code, if you only want to store the date, and not the time, you should use the datatype date and not datetime in your SQL table, as @Yanga suggests in the answer below. In EF you may specify this using the attribute [Column(Typename="date")], take a look at the answer in this question for more information: http://stackoverflow.com/questions/33762292/store-only-date-in-database-not-time-portion-c-sharp – Michael Nov 15 '16 at 13:23

4 Answers4

2

It would be silly and counterproductive to store dates as "ddMMyyyy". First of all you'd need a varchar(8), not a DATE or DATETIME.

On top of that, how are you ever going to sort it using ORDER BY, or use BETWEEN queries, or do myDate > someValue / myDate < someValue queries? You can't with a date-string formatted like that.

Also a notation such as "ddMMyyyy" is a User Interface representation of an underlying value. Databases should almost never store User Interface representations, that is a job for the... you guessed it... User Interface.

Best to just forget about it, or else be ready to face the horrible consequences.

Peter B
  • 18,964
  • 5
  • 26
  • 60
  • +1; there are still vendors who consistently store dates as strings in yyyyMMdd format, take for example the [DATS](http://help.sap.com/saphelp_nw70/Helpdata/en/fc/eb2fd9358411d1829f0000e829fbfe/content.htm) storage type in SAP's ABAP environment, which used to go to the database layer as a char(8). While this may be due to historical/backwards compatibility/portability reasons and is not recommended, it shows that certainly not everybody would deem it silly in first place (for ddMMyyyy, I agree though). – Cee McSharpface Nov 16 '16 at 13:31
0

Change you SQL Server Database data type from "datetime" to "date" ?

Yanga
  • 2,395
  • 1
  • 26
  • 27
  • this would truncate a time part. but it does in no way enforce the particular display format ddMMyyyy. – Cee McSharpface Nov 15 '16 at 13:22
  • @dlatikay His problem is not with displaying the data, but with storing it, and he tries to store it using ToString() to only get the date and not the time. As Yanga suggests, he should instead use the datatype date in the SQL table column. Look at my comment on the question for more information. – Michael Nov 15 '16 at 13:26
  • indeed. or consequently use `ToString("ddMMyyyy")` in the presentation layer. or [this](http://stackoverflow.com/questions/6121271/how-to-remove-time-portion-of-date-in-c-sharp-in-datetime-object-only), or store `DateTime.Now.Date` instead of `DateTime.Now`. all guesswork until the OP is improved. – Cee McSharpface Nov 15 '16 at 13:31
  • @dlatikay My problem is not about the ToString, When I tries the DateTime.Now to add a date to DB using EntityFramework the date is saved to MMddyyyy format. Instead I need to store the date by ddMMyyyy format without using ToString() – abdulkhadar7 Nov 16 '16 at 13:01
  • ok. this is a fundamental misunderstanding about the difference between the internal representation of values of the datetime type, and how it is displayed in different applications. the format is not a property of the type itself - neiher in .NET nor in SQL. please refer to Jon Skeet's comment to your OP, I don't know how to say it more plainly. – Cee McSharpface Nov 16 '16 at 13:14
0

From the article for the DateTime.Parse() method (MSDN Article)

So you can do:

var dateTime = DateTime.Parse("01012001");

Which will give you a DateTime typed object.

If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)

Which you would use in a situation like this (Where you are using a British style date format):

string[] formats= { "ddMMyyyy" }
var dateTime = DateTime.ParseExact("01012001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
Harshal Bhamare
  • 352
  • 1
  • 6
  • 16
0

The format "ddMMyyyy" (or any other format) makes any sense only if we're talking about strings.

If you need only the day, month, and year in the program you can still use the DateTime class. You simply ignore the other properties like minutes, hours, etc...

DateTime now = DateTime.Now;
Method(now.Day, now.Month, now.Year);

or

DateTime emptyDateTime = new DateTime();

// in this case emptyDateTime values are the following:
emptyDateTime.Year:   1
emptyDateTime.Month:  1
emptyDateTime.Day:    1
emptyDateTime.Hour:   0
emptyDateTime.Minute: 0
emptyDateTime.Second: 0
Mitulát báti
  • 1,178
  • 3
  • 15
  • 30