Problem with Date formatting
-
Hi, This is a code to get the starting and ending week dates. i.e week starting from Monday - Sunday. I want the dates in dd/MM/yyyy format. When I debug the code, it shows me the date in strFromdate and strTodate in dd/MM/yyyy format. But when it writes to the label in the aspx page it shows in the format MM/dd/yyyy hh:mm:ss format. Can anyone let me know where i m going wrong...??? DateTime dtNow = DateTime.Parse(DateTime.Today.ToString("MM/dd/yyyy")); int ToDayNumber = DayOfWeekNumber(dtNow.DayOfWeek); FromDate= dtNow.AddDays(1 - ToDayNumber); ToDate=dtNow.AddDays(7 - ToDayNumber); string strFromDate = FromDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); string strToDate = ToDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); Thanks
-
Hi, This is a code to get the starting and ending week dates. i.e week starting from Monday - Sunday. I want the dates in dd/MM/yyyy format. When I debug the code, it shows me the date in strFromdate and strTodate in dd/MM/yyyy format. But when it writes to the label in the aspx page it shows in the format MM/dd/yyyy hh:mm:ss format. Can anyone let me know where i m going wrong...??? DateTime dtNow = DateTime.Parse(DateTime.Today.ToString("MM/dd/yyyy")); int ToDayNumber = DayOfWeekNumber(dtNow.DayOfWeek); FromDate= dtNow.AddDays(1 - ToDayNumber); ToDate=dtNow.AddDays(7 - ToDayNumber); string strFromDate = FromDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); string strToDate = ToDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); Thanks
-
this is how i write in aspx page (sorry ... its in a td, not in a label) <%=FromDate%> to <%=ToDate%> Thanks
-
this is how i write in aspx page (sorry ... its in a td, not in a label) <%=FromDate%> to <%=ToDate%> Thanks
Because you use the FromDate, ToDate objects of the DateTime in the expression, so at runtime the ASP.NET will call the DateTime.ToString() method without specifying the format pattern in the method paramater. As a result you will see something that you don't expect, instead you can try with one of the two options below: + Use the objects of the string type:
<%= strFromDate %> to <%= strToDate%>
+ Use the objects of the DateTime type:
<%= FromDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")) %>
to <%= ToDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB"))%>