How to check dates in asp.net

I had this problem with dates with "ever"

I live in a country where we use British date formats, for example. dd / mm / yyyy, but every time I try to execute a query to sql db hosted on an American server, 100% of the time when I come up with errors.

Convert.ToDateTime("2007-17-5")throws an error, where as Convert.ToDateTime("2007-5-17")also produces an error.

Many validation methods, many t-sql queries, and many other methods that I tried to partially solve in most of my projects, however, I would like to know if anyone has a universal way to convert strings to a date when it does not cause any problems?

or any good resources for working with dates?

----------- editing ... -----------------

even if I format the dates correctly, the query to the sql server does not give any value, even if I'm sure that there is .. for example .. if I look for entries related to dates between 1/1/2009 (i.e. January 1, 2009) until 1/5/2009 (this is May 1, 2009), entries are not returned. And when I try to change the date in the sql server query builder, it gives me an error saying this is not a valid date that I entered ...

+3
source share
3 answers

2007-5-17 is not a valid international date (ISO 8601), 2007-05-17 . If you intend to use the CCYY-MM-DD international format , you need to enable the leading 0 s

http://www.w3.org/QA/Tips/iso-date

http://www.saqqara.demon.co.uk/datefmt.htm

+2

.

-, webconfig :

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" />

CompareValidator , Type "Date".

<CompareValidator ID="id" runat="server"
  ControlToValidate="controltovalidateid"
  ErrorMessage="Invalid Date"
  Type="Date" />

, ASP.NET. - :

<ajaxToolkit:Calendar runat="server"
  TargetControlID="Date1"
  CssClass="ClassName"
  Format="dd/mm/yyyy"
  PopupButtonID="Image1" />

+2

Source: https://habr.com/ru/post/1707158/


All Articles