Click here to Skip to main content
15,886,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
i have date in "dd:mm:yyyy hh:mm:ss" format, i want to convert date in mm:dd:yyyy format
using IFormatProvider/System.Globalizationsystem
how can i do this?
thanks
Posted
Updated 23-May-11 7:09am
v3

similar topic
DateTime Conversion C#[^]

C#
string myDate = "15:06:2010";
            DateTime dt;

            //verify given date is valid or not
            if (DateTime.TryParseExact(myDate, "dd:MM:yyyy", CultureInfo.InvariantCulture,     System.Globalization.DateTimeStyles.None, out dt))
            {
               //Valid date format
               MessageBox.Show(dt.ToString("MM:dd:yyyy"));

            }
            else
            {
             //invalid date format
            }
 
Share this answer
 
v4
Comments
niravsahayata 24-May-11 13:42pm    
its not working..as i am giving date to 15:06:2010, its throwing me error.."date is not in correct format"
ambarishtv 25-May-11 1:43am    
hi,
its working here ... check with this code :)

[no name] 25-May-11 8:39am    
Change the first occurence of "mm:dd:yyyy" to "dd:MM:yyyy" and the second to "MM:dd:yyyy" and it works. Lower case mm refers to minutes, upper case to month.
ambarishtv 25-May-11 9:22am    
ok , i updated it
Thank You

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
DateTime dt = DateTime.Now;
        string Str = dt.ToString("mm:dd:yyyy");
 
Share this answer
 
C#
var dt = DateTime.UtcNow.ToString("dd:MM:yyyy hh:mm:ss"); // Date to string
var nDt = DateTime.ParseExact(dt, "dd:MM:yyyy hh:mm:ss", CultureInfo.InvariantCulture); // String to datetime
 
var sdt = nDt.ToString("MM:dd:yyyy"); // Back to formatted string
 
Share this answer
 
C#
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
 dateInfo.ShortDatePattern = "dd/MM/yyyy";//i am getting date in this format 
 DateTime temp;
 string toDate = txtToDate.Text;
 temp = Convert.ToDateTime(toDate, dateInfo); 
 toDate = temp.ToString("MM/dd/yyyy").Trim();//converting dd/MM/yyyy to MM/dd/yyyy
 
Share this answer
 
v2
hi ..
Your can try it..
string stDate=string.format(DateTime.Now,"{0:dd/MM/yyyy}");
 
Share this answer
 
v2
Comments
niravsahayata 24-May-11 13:35pm    
i want in mm:dd:yyyy format..by using this method it will throw an error
string dt= string.Format("{0:MM-dd-yyyy}", DateTime.Now.Date);
 
Share this answer
 
DateTime date = DateTime.Now;
string d = date.ToString("mm-dd-yyyy");
 
Share this answer
 
v2
Try the below Tip which has code snippet and explanation,

DateTime format in .NET 4.0[^]
 
Share this answer
 
v2

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