I have one class for formating date in my web app. The date should be the same for every object in app and should be as a NEWSTRING
public const string NEWSTRING = "dd/MM/yy HH:mm";
public static string FormatDate(DateTime date) {
return date.ToString(NEWSTRING);
}
When I try to convert for example
Text1.Text = FormatDate(currentDate);
where currentDate is:
DateTime currentDate = "yyyy-mm-dd hh:mm:ss"
I get as the result dd-mm-yy hh:mm instead of dd/MM/yy HH:mm
How can I get the date in format dd/MM/yy HH:mm by using my FormatDate ?
Mike GoldPosted Aug 19, 2007, 11:21 PM
This should solve your problem. Use the custom date time parse method in this post.
class Program{
static void Main(string[] args){
DateTime dt = ParseUniqueDateTimeFormat("2007-18-07 23:15"); Console.WriteLine(FormatDate(dt)); Console.ReadLine();}
public const string NEWSTRING = "dd/MM/yy HH:mm"; public static string FormatDate(DateTime date) { return date.ToString(NEWSTRING);}
// parse this format into a date time // "2007-18-07 23:15" static DateTime ParseUniqueDateTimeFormat(string uniqueDateString){
DateTime result = new DateTime(); try{
string[] dateAndTime = uniqueDateString.Split(' '); string[] dateParams = dateAndTime[0].Trim().Split('-'); string[] timeParams = dateAndTime[1].Trim().Split(':'); int year = Convert.ToInt32(dateParams[0]); int month = Convert.ToInt32(dateParams[2]); int day = Convert.ToInt32(dateParams[1]); int hour = Convert.ToInt32(timeParams[0]); int minute =Convert.ToInt32(timeParams[1]);result =
new DateTime(year, month, day, hour, minute, 0);}
catch (Exception ex){
Console.WriteLine("Couldn't Parse {0}", uniqueDateString);}
return result;}
}
Mike GoldPosted Aug 19, 2007, 11:05 PM
Still not sure you are describing the problem correctly. I tried the following code and it does exactly what you expect. The problem I think you are having is that .NET can't parse "2007-18-07 23:15" into a DateTime. It doesn't recognize the format. You'll need to parse this manually yourself. In the next reply, I'll show you how to do this.
-Mike
class Program{
static void Main(string[] args){
// DateTime dt = Convert.ToDateTime("2007-18-07 23:15");
DateTime dt = new DateTime(2007, 7, 18, 23, 15, 0); Console.WriteLine(FormatDate(dt)); Console.ReadLine();}
public const string NEWSTRING = "dd/MM/yy HH:mm"; public static string FormatDate(DateTime date) { return date.ToString(NEWSTRING);}
}
VinniePosted Aug 19, 2007, 2:41 PM
I want to produce "18/07/07 23:15" at the end.
I read date from grid field where date is "2007-18-07 23:15:01", and want to use FormatDate to produce "18/07/07 23:15" in the textbox.
Text1.Text = ClassName.FormatDate(Convert.ToDateTime(dv.Table.Rows[0][0]));
or
Text1.Text = ClassName.FormatDate("2007-18-07 23:15");
ClassName contains
public const string NEWSTRING = "dd/MM/yy HH:mm";
public static string FormatDate(DateTime date) {
return date.ToString(NEWSTRING);
}
but this code produce this "18-07-07 23:15" and I have many grids with dates like decribed before, so I should change somehow ClassName.FormatDate.
Mike GoldPosted Aug 19, 2007, 12:16 AM
Not sure I understand your question. DateTime doesn't have a format until you use the ToString method on it. The DateTime class is a numerical representation of your date.
so DateTime myDateTime = new DateTime(year, month, day, hour, minute, second);
myDateTime.ToString("yyyy-MM-dd HH:mm") will produce a string with the format "2007-18-07 23:15" for e.g.