I have a webform which accepts DATE in "DD/MM/YYYY"
. I want to store it in my MS SQL database in "SQL accepted date format".
I also have another page which displays summary of my table in another webform, where I do not want to display date in "YYYY/MM/DD"
. could someone show some pointers
Asked
Active
Viewed 231 times
0

Ameya Deshpande
- 3,580
- 4
- 30
- 46

Manikandan Soubramaniane
- 19
- 1
- 9
-
2`DateTime` objects **does not have format**. You should save your date as `DateTime` objects, then when displaying it - format. – SᴇM Oct 24 '18 at 11:35
-
Possible duplicate of [How to insert DateTime in MySql Database using C# code](https://stackoverflow.com/questions/4153799/how-to-insert-datetime-in-mysql-database-using-c-sharp-code) – Drag and Drop Oct 24 '18 at 11:35
-
and https://stackoverflow.com/questions/29181975/how-to-insert-date-into-sql-database-date-column-using-datetimepicker – Drag and Drop Oct 24 '18 at 11:36
-
@DragandDrop The first duplicate is bad because it shows how to use "correct" string formatting to pass values, whereas the correct way is not to format at all and pass the original values. The other is better. – Lasse V. Karlsen Oct 24 '18 at 12:07
-
@LasseVågsætherKarlsen, from the 5 first page of Google I found no answer that show both method with enought detail to qualify as the perfect dupe target. While I would prefer Op using Params, I take op's "Date format" as string format for `DateTime`. And because it's a direct answer to my interpretation of the question I choose this one. – Drag and Drop Oct 24 '18 at 12:13
1 Answers
3
In the database: do not store dates as strings. Store them as datetime
, or date
, or datetime2
.
In your app's SQL code: do not pass dates as strings. Pass them as DateTime
parameters
In your app's main code logic: do not pass dates as strings. Pass them as DateTime
So; in your app's UI-level code:
- when taking input, use
DateTime.Parse
(etc) to convert the user's input to aDateTime
before passing it down; if that meansParseExact
/TryParseExact
specifying"dd/MM/yyyy"
- then fine - when displaying values from the DB, use the type's formatting features to display it in any way you choose

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
If your application is "inter-technological", then you can convert date to epoch timestamp and use it on the edges of systems. – Fka Oct 24 '18 at 11:46
-
1@Fka the real question there is: what epoch and scale? unix time is just one option of many; but yes, anything that isn't a string is good – Marc Gravell Oct 24 '18 at 11:56
-
Marc - I assumed we talk about dates, not datetimes or anything below days. Can you provide examples of *other options*? – Fka Oct 24 '18 at 11:58
-
1@Fka sure; you could talk seconds or milliseconds since 1970 (even for whole days), you could talk ticks from "common era" (i.e. the BCE/CE divide); you could talk "days since 2015" (if your company started in 2015, for example) - or "hours since 2015" - tons of options - entirely arbitrary, as long as both sides agree on what it means – Marc Gravell Oct 24 '18 at 12:00