Abhishek gupta
Abhishek gupta

Reputation: 463

Incorrect Date Format and date generates

I have variable of a type DateTime objApplicationSummaryInfo.AdmissionDate. I am trying to assign a value, such as

objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
       TextBox3.Text.ToString(), "dd/mm/yyyy", null);

But when i am assigning a value like 27/09/2012to a textbox3, the variable objApplicationSummaryInfo.AdmissionDate takes a value 1/27/2012 12:00:09. The format as well as the date comes back incorrect.

What possible code am I missing and what can be an alternate solution. Thanks for assistance.

Upvotes: 1

Views: 558

Answers (4)

rohan panchal
rohan panchal

Reputation: 881

hey are you Testing on localHost try it on Server i will work on server but not on localhots

Upvotes: 0

ChrisF
ChrisF

Reputation: 137158

mm is minutes.

MM is months

Custom Date and Time Format Strings

Your code should be:

objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
   TextBox3.Text.ToString(), "dd/MM/yyyy", null);

You will also need to set the culture:

CultureInfo provider = CultureInfo.InvariantCulture;

objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
   TextBox3.Text.ToString(), "dd/MM/yyyy", culture);

Otherwise it will use the culture of the machine running the code.

Upvotes: 2

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28990

You can try with - dd/MM/yyyy

DateTime.ParseExact(TextBox3.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);

Upvotes: 1

Oded
Oded

Reputation: 499132

"dd/mm/yyyy" should probably be "dd/MM/yyyy"

  • mm - is for minutes
  • MM - is for months

objApplicationSummaryInfo.AdmissionDate = DateTime.ParseExact(
       TextBox3.Text.ToString(), "dd/MM/yyyy", null);

Upvotes: 1

Related Questions