Asked by:
FormatDateTime ?

Question
-
User898263470 posted
Hello I am writing a helper function that I wish to return a formatted date (mm/dd/yyyy). First, I have searched these boards and MSDN up and down and cannot find a good example. I am looking for something similiar to the VBScript FormatDateTime function in C#. I have seen examples of the ToString and Parse functions formatting dates, but to be honest...I just don't get them. Here is how my function is formatted.public string Format_The_Date(DateTime input) { DateTime strDate = DateTime.Parse(input); dt = strDate.ToString("MM/dd/yyyy"); return strDate; }
Now this does not work, but it is what I am looking to accomplish. Can anyone help? TIAWednesday, August 20, 2003 1:04 PM
All replies
-
User1277840654 posted
did you try Format(strDate,"MM/dd/yyyy")? HTH -akaWednesday, August 20, 2003 1:14 PM -
User-1174460629 posted
Try MyString = CDate("1/1/2003").ToShortDateStringWednesday, August 20, 2003 1:21 PM -
User898263470 posted
Format(strDate,"MM/dd/yyyy") = Format does not exist in the class or namespace KB.Article. I looked at MSDN and it seems this is part of the System.Windows.Forms namespace....but VS.Net does not recognize that namespace. MyString = CDate("1/1/2003").ToShortDateString = The name CDate does not exist in the class or namespace KB.Article. I do not believe that CDate is a C# function either. Thanks for trying.Wednesday, August 20, 2003 1:47 PM -
User1277840654 posted
Format is a function that is part of VB language. I am not sure why you are looking for a namespace here. did you try using this in your code. if so, did you get an error? -akaWednesday, August 20, 2003 2:23 PM -
User1172191166 posted
You and other almost has it :) The code should look like:public string Format_The_Date(DateTime input) { return input.ToShortDateString(); }
Wednesday, August 20, 2003 2:23 PM -
User-912481521 posted
You have return type as string and in code it returns a DateTime TryResponse.Write (Format_The_Date ("12/09/2003")); =================================== public string Format_The_Date(string input) { string dt; DateTime strDate = DateTime.Parse(input); dt = strDate.ToString("yyyy/MM/dd");//Specify Format you want the O/P as... return dt; }
Wednesday, August 20, 2003 2:27 PM -
User898263470 posted
=== aka Format is a function that is part of VB language. I am not sure why you are looking for a namespace here. Im not using VB. Im using C# === === daibatronpublic string Format_The_Date(DateTime input) { return input.ToShortDateString(); }
Gives me this error. Compiler Error Message: CS1502: The best overloaded method match for 'KB.Article.Format_The_Date(System.DateTime)' has some invalid arguments Source Error: Line 36: Line 37: Posted On: Line 38: <%# Format_The_Date(DataBinder.Eval(Container.DataItem, "posted_date")) %> Line 39: By: <%# DataBinder.Eval(Container.DataItem, "posted_by") %> Line 40: Article # === === SushilaSBpublic string Format_The_Date(string input) { string dt; DateTime strDate = DateTime.Parse(input); dt = strDate.ToString("yyyy/MM/dd");//Specify Format you want the O/P as... return dt; }
Gives me the following error Compiler Error Message: CS1502: The best overloaded method match for 'KB.Article.Format_The_Date(string)' has some invalid arguments Source Error: Line 36: Line 37: Posted On: Line 38: <%# Format_The_Date(DataBinder.Eval(Container.DataItem, "posted_date")) %> Line 39: By: <%# DataBinder.Eval(Container.DataItem, "posted_by") %> Line 40: Article # === Does it have anything to do with my input, which is in a repeater as such:<%# Format_The_Date(DataBinder.Eval(Container.DataItem, "posted_date")) %>
Thanks for all your help and patience. This is my first week coding .NET and C#.Wednesday, August 20, 2003 2:50 PM -
User1277840654 posted
My fault. i did not realize that you are using C#. Sorry. -akaWednesday, August 20, 2003 3:01 PM -
User898263470 posted
Dont be silly aka. I appreciate the help.Wednesday, August 20, 2003 3:58 PM -
User1172191166 posted
Paulha, Your function requires a DateTime argument, but you probably pass in a date in string format from the database. You can rewrite the function to accept a string argument, instead of DateTime like this: public string Format_The_Date( string inputDate ) { DateTime dt = DateTime.Parse( inputDate ); return dt.ToShortDateString(); }Wednesday, August 20, 2003 4:21 PM -
User1172191166 posted
And I forgot to add that your error could happen if the column "posted_date" has a null or empty value.Wednesday, August 20, 2003 4:26 PM -
User-912481521 posted
Try giving <%# Format_The_Date(DataBinder.Eval(Container.DataItem, "posted_date").ToString()) %> HTHWednesday, August 20, 2003 4:46 PM -
User-260387219 posted
Change:<%# Format_The_Date(DataBinder.Eval(Container.DataItem, "posted_date")) %>
To:<%# DataBinder.Eval(Container.DataItem, "posted_date", "{0:MM/dd/yyyy}") %>
There is no need for an additional function because you can pass the formatter in the DataBinder.Eval as above...but if you wanted to do a function:<%# Format_The_Date((String)(DataBinder.Eval(Container.DataItem, "posted_date"))) %>
but using the formatter as i suggest is a better way.Wednesday, August 20, 2003 5:18 PM -
User1481037074 posted
dim strDateNow as string = DateTime.Now.Month.ToString.PadLeft(2, "0") + "/" + DateTime.Now.Day.ToString.PadLeft(2, "0") + "/" + DateTime.Now.Year.ToString.PadLeft(2, "0000") dim strTimeNow = Format(DateTime.Now, "HH:mm:ss").ToString fdo.Wednesday, August 20, 2003 8:15 PM -
User-1174460629 posted
Ah-ha! I didn't realize you were using C#, bud. Sorry, I wouldn't have used CType!Wednesday, August 20, 2003 11:30 PM -
User898263470 posted
Thanks everyone for all the help. I used heuertk's method and it works great!!Thursday, August 21, 2003 11:08 AM