Question

i am picking the value from calender extender in textbox and i am getting the value in the format {"MM/dd/yyyy"} but i want it in the format {"dd/MM/yyyy"} in another textbox ( txt_actualrightformat.Text) as code shown below

DateTime wrongformat = DateTime.Parse(TextBox4.Text);
String rightformat = String.Format("{0:dd/MM/yyyy}", wrongformat.Date);
txt_actualrightformat.Text = rightformat.ToString();
Was it helpful?

Solution

DateTime is irespective of the format, format is only for displaying purpose. If you are not getting the right date in wrongformat then you can use DateTime.ParseExact with the format. and then simply

txt_actualrightformat.Text = wrongformat.ToString("dd/MM/yyyy");

EDIT:

use DateTime.ParseExcat like:

DateTime dt = DateTime.ParseExact(TextBox4.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);
txt_actualrightformat = dt.ToString("dd/MM/yyyy");

OTHER TIPS

try these

DateTime wrongformat = DateTime.Parse(TextBox4.Text);

txt_actualrightformat.Text =wrongformat.ToString("dd'/'MM'/'yyyy");

or

 txt_actualrightformat.Text =String.Format(CultureInfo.InvariantCulture, "{0:dd/MM/yyyy}", _wrongformat )

update:

i think the date in the TextBox4is really in the wrongformat :-)
Note that "22/3/2013" It matches the format "d/M/yyyy" and doesn't match the format "dd/MM/yyyy". - for "dd/MM/yyyy" it should be "22/03/2013".

DateTime dt;

if(DateTime.TryParseExact(TextBox4.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                           DateTimeStyles.None, out td))
{
  // Valid date used in `TextBox4` (NOTE : dd/MM/yyyy)!, you can use dt now as i explained above!.:-)
}

Before parsing or converting any datetime value check your calendar's date format. on your aspx page go to the calendarExtender and go to its properties then find format and set the format to dd/MM/yyyy and todays date format also do the same. Then from your code behind declare a variable in datetime type and simply pass the text box value which calendarExtender puts the date on and convert the textbox value in date time. The code will be like public DateTime date{get;set;}

date = Convert.ToDateTime(txtDate.Text.ToString()); In sql server the value will save like "2016-02-09 00:00:00.000" this format

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top