Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi,

I need to change the date format manually for a given input from a text box.
The input value will be formatted as a dd-mm-yyyy - String, and the output shall be displayed as dd/mm/yyyy-String.

Thank you.
Posted
Updated 17-Dec-13 5:30am
v2
Comments
ShridharPanigrahi 17-Dec-13 5:49am    
Consider using a calendar control

try this:-

string strFormatDt = Convert.ToDateTime(txtDt.Text).ToString("dd/MM/yyyy");
 
Share this answer
 
v2
Comments
Mr.VJ 17-Dec-13 6:48am    
here getting error in Strings.Format(Convert.ToDateTime(txtDate.Text), "dd/MM/yyyy");

it is Strings or anything else ?
TrushnaK 17-Dec-13 7:03am    
Which error you get?
Mr.VJ 17-Dec-13 7:09am    
Strings
TrushnaK 17-Dec-13 8:02am    
i have updated ans plz check.
Marco Bertschi 17-Dec-13 11:28am    
This solution will perform quite bad, since you need a method call for Convert and ToString (You are essentially converting a string to a date, and back to a string again!) - Why not use String.Format, which is a single method call?
Nevermind, I have overseen that the input comes from a textbox - Your solution seems fine to me.
[MarcoB]
This solution will perform badly, since for dateTime.Year, dateTime.Month and dateTime.Day is a method and a property get operation called.
[/MarcoB]

look at this:
C#
private  string ConvertDatetimeToValid(DateTime dateTime)
       {
           string datetime = string.Concat(dateTime.Day.ToString(), "/", dateTime.Month.ToString(), "/", dateTime.Year.ToString());
 
           return datetime;
       }
 
Share this answer
 
v2
Comments
Marco Bertschi 17-Dec-13 11:26am    
This solution will perform badly, since for dateTime.Year, dateTime.Month and dateTime.Day is a method and a property get operation called. Why not use String.Format?
Hi,

after browsing this page[^] I found the following solution which helps you:


C#
DateTime dt = DateTime.Now;
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

 
Share this answer
 
in my idea..

use mask textbox..

1.select and drag masktextbox from toolbox
2.and cleck the masktextbox they have a arrow mark in above
3.click that one,they have a option like " set mask "
4.and click,select the " sort date " and click ok
5.run

i think this useful for you!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900