Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

How to set DateTime property using yyyy-MM-dd format

$
0
0

I have a datetime property similar to the following:

private DateTime birthDate;
public DateTime BirthDate
{
    get { return birthDate; }
    set { birthDate = value; }
}

For displaypurposes, I use a datetime picker with "dd-MM-yyyy" format. However, to actually set the property, I want to use "yyyy-MM-dd" format. I am trying the following code but I keep receiving "String was not recognized as a valid DateTime":

BirthDate = DateTime.ParseExact (txtDateOfBirth.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);

The reason I want to use yyyy-MM-dd format for setting the property is that the property value will be saved in SQL Server whichs accepts (yyyy-MM-dd) format irrespective of culture used.


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>