.net获取星期几并按开始日期排序

时间:2018-07-25 10:26:22

标签: c# .net c#-4.0 datetime-format

通过在班级模型中插入文字来获取缩写的天名:

        var daysNames = new CultureInfo(culture).DateTimeFormat
            .AbbreviatedDayNames
            .Select((item) => new CalendarDaysItem(item.ToUppercaseFirst()))
            .ToList();

enter image description here

但是我需要使用特定的工作日对列表进行排序,请尝试以下示例:

        var firstDayOfWeek = new CultureInfo(culture).DateTimeFormat.FirstDayOfWeek;

        var daysOfWeek = Enum.GetValues(typeof(DayOfWeek))
            .Cast<DayOfWeek>()
            .OrderBy(x => (x - firstDayOfWeek + 7) % 7);

        var daysOfWeekOrdered = daysOfWeek.OrderBy(x => (x - firstDayOfWeek + 7) % 7);

enter image description here

但是文化是EN-US,我需要按参数进行更改

2 个答案:

答案 0 :(得分:1)

我认为这可以解决您的问题。请尝试以下代码:

var dateTimes = timeBands.OrderBy(x =>((int)x.DayOfWeek + 6)%7)                                 .ToList()

有关更多信息,请参考:.OrderBy(DayOfWeek) to treat Sunday as the end of the week

答案 1 :(得分:0)

            Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-PT");
            DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
            var daysOfWeek = Enum.GetValues(typeof(DayOfWeek))
               .Cast<DayOfWeek>()
               .OrderBy(x => (x - firstDayOfWeek + 7) % 7)
               .Select(x => DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName(x))
               .ToList();