Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A less ugly way to localize DayOfWeek? [duplicate]

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

like image 471
bevacqua Avatar asked Dec 22 '22 14:12

bevacqua


2 Answers

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
like image 100
FerranB Avatar answered Dec 24 '22 02:12

FerranB


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)" : "");  
like image 44
Rob Stevenson-Leggett Avatar answered Dec 24 '22 03:12

Rob Stevenson-Leggett