开发者

A less ugly way to localize DayOfWeek? [duplicate]

This question already has answers here: C# Cultures: Localize DayOfWeek? (2 answers) 开发者_如何转开发 Closed 9 years ago.
using System;

namespace Server.Custom.Extensions
{
    public static class FriendlyExtensions
    {
        public static string Friendly(this DayOfWeek day)
        {
            if (day == DateTime.Now.DayOfWeek)
                return "Hoy";

            int dayOfWeek = (int)DateTime.Now.DayOfWeek;
            int dayOfEvent = (int)day;

            if (dayOfWeek + 1 == dayOfEvent || (dayOfWeek == 6 && dayOfEvent == 0))
                return "Mañana";

            switch (day)
            {
                default:
                case DayOfWeek.Monday: return "Lunes";
                case DayOfWeek.Tuesday: return "Martes";
                case DayOfWeek.Wednesday: return "Miercoles";
                case DayOfWeek.Thursday: return "Jueves";
                case DayOfWeek.Friday: return "Viernes";
                case DayOfWeek.Saturday: return "Sabado";
                case DayOfWeek.Sunday: return "Domingo";
            }
        }
    }
}

Is there some way to localize this with Cultures? how? :( By the way I want it to say "Today" or "Tomomorrow" too, not just convert the days


DateTime.Now.ToString("ddd", new CultureInfo("es-ES"));

or

DateTime.Now.ToString("dddd", new CultureInfo("es-ES"));

References:

  • DateTime.ToString Method (String)
  • How to: Extract the Day of the Week from a Specific Date


This code from here (see bottom) might put you on the right track.

 CultureInfo german = new CultureInfo("de-DE");
 DateTimeFormatInfo dtfi = german.DateTimeFormat;

 Console.WriteLine("Days of the week for the {0} culture:",
                    german.Name);
 for (int ctr = 0; ctr < dtfi.DayNames.Length; ctr++)
    Console.WriteLine("   {0,-12}{1}", dtfi.DayNames[ctr],
                      dtfi.DayNames[ctr] == dtfi.DayNames[(int)dtfi.FirstDayOfWeek] ? 
                            "(First Day of Week)" : "");  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜