有没有办法根据使用.NET框架的不同文化找到构成周末或工作周的日子?例如,一些穆斯林国家从周日到周四有一个工作周。
答案 0 :(得分:3)
我唯一知道的是如何让这一周开始。也许这有助于:
CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
来自System.Globalization
的,也许你会在这个包里找到一些东西。
有几个日历类,如JulianCalendar, HebrewCalendar
等。有可能找到你想要的东西。
答案 1 :(得分:3)
没有人有解决方案,所以我写了一个。这使用国家来确定一天是工作日,周末还是1/2工作日(某些国家/地区的星期六)。这方面有些含糊不清,因为墨西哥星期六的半天是“习惯性的”,但不是正式的。对于这样的情况,我将其设置为工作时间。
这包括除马来西亚其他3个省份以外的所有省份,这些省份与马来西亚其他省份不同。 AFAIK,CultureInfo.Name对这3个省没有明显的价值。最有趣的国家,文莱周末是周五和周四星期天,星期六是工作日。
代码可以Is it the weekend下载为项目吗?主要代码如下:
using System;
using System.Globalization;
namespace windward
{
/// <summary>
/// Extensions for the CultureInfo class.
/// </summary>
public static class CultureInfoExtensions
{
/// <summary>
/// The weekday/weekend state for a given day.
/// </summary>
public enum WeekdayState
{
/// <summary>
/// A work day.
/// </summary>
Workday,
/// <summary>
/// A weekend.
/// </summary>
Weekend,
/// <summary>
/// Morning is a workday, afternoon is the start of the weekend.
/// </summary>
WorkdayMorning
}
/// <summary>
/// Returns the English version of the country name. Extracted from the CultureInfo.EnglishName.
/// </summary>
/// <param name="ci">The CultureInfo this object.</param>
/// <returns>The English version of the country name.</returns>
public static string GetCountryEnglishName(this CultureInfo ci)
{
string[] parts = ci.EnglishName.Split(new[] {'(', ')'}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
return ci.EnglishName;
parts = parts[1].Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
return parts[parts.Length - 1].Trim();
}
/// <summary>
/// Returns the English version of the language name. Extracted from the CultureInfo.EnglishName.
/// </summary>
/// <param name="ci">The CultureInfo this object.</param>
/// <returns>The English version of the language name.</returns>
public static string GetLanguageEnglishName(this CultureInfo ci)
{
string[] parts = ci.EnglishName.Split(new[] {'('}, StringSplitOptions.RemoveEmptyEntries);
return parts[0].Trim();
}
/// <summary>
/// Return if the passed in day of the week is a weekend.
///
/// note: state pulled from http://en.wikipedia.org/wiki/Workweek_and_weekend
/// </summary>
/// <param name="ci">The CultureInfo this object.</param>
/// <param name="day">The Day of the week to return the stat of.</param>
/// <returns>The weekday/weekend state of the passed in day of the week.</returns>
public static WeekdayState IsWeekend(this CultureInfo ci, DayOfWeek day)
{
string[] items = ci.Name.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
switch (items[items.Length - 1])
{
case "DZ": // Algeria
case "BH": // Bahrain
case "BD": // Bangladesh
case "EG": // Egypt
case "IQ": // Iraq
case "IL": // Israel
case "JO": // Jordan
case "KW": // Kuwait
case "LY": // Libya
// Northern Malaysia (only in the states of Kelantan, Terengganu, and Kedah)
case "MV": // Maldives
case "MR": // Mauritania
case "NP": // Nepal
case "OM": // Oman
case "QA": // Qatar
case "SA": // Saudi Arabia
case "SD": // Sudan
case "SY": // Syria
case "AE": // U.A.E.
case "YE": // Yemen
return day == DayOfWeek.Thursday || day == DayOfWeek.Friday
? WeekdayState.Weekend
: WeekdayState.Workday;
case "AF": // Afghanistan
case "IR": // Iran
if (day == DayOfWeek.Thursday)
return WeekdayState.WorkdayMorning;
return day == DayOfWeek.Friday ? WeekdayState.Weekend : WeekdayState.Workday;
case "BN": // Brunei Darussalam
return day == DayOfWeek.Friday || day == DayOfWeek.Sunday
? WeekdayState.Weekend
: WeekdayState.Workday;
case "MX": // Mexico
case "TH": // Thailand
if (day == DayOfWeek.Saturday)
return WeekdayState.WorkdayMorning;
return day == DayOfWeek.Saturday || day == DayOfWeek.Sunday
? WeekdayState.Weekend
: WeekdayState.Workday;
}
// most common Saturday/Sunday
return day == DayOfWeek.Saturday || day == DayOfWeek.Sunday ? WeekdayState.Weekend : WeekdayState.Workday;
}
}
}
答案 2 :(得分:0)
我不会给你一个.NET答案,但我会说你不会以“文化”为基础,而是以国家为基础。
您可以从知识产权中获得国家的准确度(但绝不会是100%)。在那之后,我会建议大量的谷歌搜索,因为我怀疑你会找到已经编写的代码。
(您也可以查看一些开源日历/约会程序,尤其是广泛使用的程序,如Linux,或者Lightning,Thunderbird插件。如果您浏览他们的代码,您可能会找到相关的数据此)
幸运的是,你只是面对一件事,而不是一件难以实施的事情。
祝你好运!答案 3 :(得分:0)
发现这个问题很有意思 - 我自己没有答案..但找到了一个有趣的资源 - 还讨论了日历。
答案 4 :(得分:0)
以下代码将一直有效,直到过去2天被视为文化周末。
:)
/// <summary>
/// Returns true if the specified date is weekend in given culture
/// is in.
/// </summary>
public static bool IsItWeekend(DateTime currentDay, CultureInfo cultureInfo)
{
bool isItWeekend = false;
DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
DayOfWeek currentDayInProvidedDatetime = currentDay.DayOfWeek;
DayOfWeek lastDayOfWeek = firstDay + 4;
if (currentDayInProvidedDatetime == lastDayOfWeek + 1 || currentDayInProvidedDatetime == lastDayOfWeek + 2)
isItWeekend = true;
return isItWeekend;
}