Thursday, March 22, 2012

Short name of the DayName

Hi everyone

I am working on date formated related.
I have to display the day name as Mon, Tue, Wed

DateTime.Now.DayOfWeek.ToString()

when im trying with this its returning like Monday , Tuesday.
how can i get the short name of the day.

can any one help me out!
i appreciate your time.
Thank you

DateTime.Now.ToString( "ddd" )


With only having 7 options... it probably wouldn't take long to whip up a quick helper function that returns the day in the format you want:

1private string FormatDayOfWeek(DateTime TheDate)2{3string strDay ="";4switch(TheDate.Now.DayOfWeek.ToString())5 {6case"Monday":7 strDay ="Mon";8break;910case"Tuesday":11 strDay ="Tue";12break;1314 .....15 .....16 }17return strDay;18}

Ugh, don't do that. There are easier ways to do it, as in my post above.

Always checkStandard DateTime Format Strings andCustom DateTime Format Strings.


"Ugh".... Geeked

I figured there was an easier way baked into the DateTime class... but I don't have that class memorized.


Another solution, which gets you the abbreviated day names for any culture.

1 using System.Globalization;
2 public partialclass TestPage : System.Web.UI.Page
3 {
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 CultureInfo ci =new CultureInfo("en-us");
7 foreach (string _dayin ci.DateTimeFormat.AbbreviatedDayNames)
8 Response.Write(_day +"<br/>");
9 }
10 }

The above code prints Sun, Mon, Tue. Pass on a different culture like (ar-ae, fr-FR, de-DE) to the CultureInfo and you will get the short names in the respective cultures.
Just digg the ci.DateTimeFormat, and you will find many useful collections.

Thanks

0 comments:

Post a Comment