C#.net date Conversion Problem
-
Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.
-
Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.
Hi For datetime function, it always take this format MM/dd/yyyy. Call this function (added below) if your date format is dd/MM/yyyy:
public string ParseDateTime(string StrDate)
{
String StrReturnDate = "";
String StrDay = "";
String StrMonth = "";
String StrYear = "";
StrDay = StrDate.Substring(0, 2);
StrMonth = StrDate.Substring((StrDate.IndexOf("/") + 1), 2);
StrYear = StrDate.Substring((StrDate.LastIndexOf("/") + 1), 4);
StrReturnDate = StrMonth + "/" + StrDay + "/" + StrYear;
return StrReturnDate;
}Harini
-
Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.
Try this:
string DATETIMEPATTERN = "dd/MM/yyyy"; string strdate = "28/02/2007"; DateTime mydatetime; DateTime dt; DateTime.TryParseExact(strdate, DATETIMEPATTERN, null, DateTimeStyles.None, out dt); mydatetime = dt;
V. I found a living worth working for, but haven't found work worth living for.
-
Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.
Hi, if you getting the date as dd/MM/YYYY format(e.g, 31/01/2007) convert it to the .NET compliant date format like, 31/JAN/2007. You can simply do so by calling the function GetMonth. string dateValue = "31/01/2007"; string month = GetMonth(dateValue.Split('/')[1]); Then perform the following DateTime.Parse(stringDateTime); You can easily set the datetime for the corresponding string public string GetMonth(string monthNumber) { string [] monthName = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Nov","Dec"}; return monthName[Int32.Parse(monthNumber)- 1]; } -- modified at 4:58 Wednesday 28th February, 2007
Mohan
-
Hello All , Is There any way to Convert the Date in one Format to Another Format I am taking Date in dd/MM/yyyy Format from my Calender to TextBox :confused::confused:Now I need to take this date from TextBox as a string and Convert to dateTime (i.e Convert.TodateTime(TextBox1.Text)) Having MM/dd/yyyy(i.e.System Specified ) Format Thanks for any help anyone is able to give me.
What you're saying doesn't make a great deal of sense as a DateTime is a DateTime is a DateTime, formatting is irrelevant. Do you actually mean that you want the string equivalent of a DateTime string value in an alternate string format? EG;
private string FormatDateTimeString( string dateTimeString ) { DateTime dateTime = DateTime.Now; if( DateTime.TryParse( dateTimeString, out dateTime ) ) { return dateTime.ToString( "MM/dd/yyyy" ); } else { throw new FormatException( "The string value, (" + dateTimeString + "), passed to the 'FormatDateTimeString( string dateTimeString )' method is not a valid DateTime" ); } }
Rhys
ELYSIUM, n. An imaginary delightful country which the ancients foolishly believed to be inhabited by the spirits of the good. This ridiculous and mischievous fable was swept off the face of the earth by the early Christians -- may their souls be happy in Heaven!
Ambrose Bierce (1842 - 1914)
Behind every argument is someone's ignorance.
Louis D. Brandeis (1856 - 1941)