Friday, November 20, 2009

ASP.NET (handling Dates)

Classic ASP can be a pain when it comes to date (the biggest problem for us Asian! US is mm/dd/yyy but we are in dd/mm/yyyy)

ASP.NET solves this problem quite well, if you know the right thing to do, i tend to expect the function to be straight forward, pls there are many ways to convert a date to a particular format. but this method seems to be the best,

My Scenario: using the (ASP.NET AJAX toolkit) calendar extender on a textbox, when submit this 'string' needs to be converted to a dateTime type.

Solution: just use DateTime.ParseExact

System.DateTime.ParseExact("28/11/2009", "dd/MM/yyyy", null);

My problem with this initially was the 3rd parameter in this function that expects 'IFormatProvider' which i really dont know what the heck im suppose to do, yo buddy! i just want to have my date in dd/MM/yyyy, why the complexity, well seems solution is just to add a 'null' there and your done.

got this solution from: thomasfreudenberg *so thanks bro*

Other DateFunction: to convert a datetime to be displayed on a page to be in a certain format. Simply:-

txtExpiryDate.Text = myDate.ToString("dd/MM/yyyy");

Also remember, the string formatter must be "dd/MM/yyyy" and NOT "dd/mm/yyyy" as "mm" means 'minutes'

Also when binding to a ListView, you can do this with Eval Method:-
<%# Eval("endTime","{0:dd/MM/yyyy}")%>


No comments: