0

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

5
  • 2
    DateTime objects does not have format. You should save your date as DateTime objects, then when displaying it - format. Commented Oct 24, 2018 at 11:35
  • Possible duplicate of How to insert DateTime in MySql Database using C# code Commented Oct 24, 2018 at 11:35
  • and stackoverflow.com/questions/29181975/… Commented Oct 24, 2018 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. Commented Oct 24, 2018 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. Commented Oct 24, 2018 at 12:13

1 Answer 1

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 a DateTime before passing it down; if that means ParseExact / 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
Sign up to request clarification or add additional context in comments.

4 Comments

If your application is "inter-technological", then you can convert date to epoch timestamp and use it on the edges of systems.
@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 - I assumed we talk about dates, not datetimes or anything below days. Can you provide examples of other options?
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.