显示中文星期几
引自:
第一种方法:直接翻译,最笨、最容易想到的方法。
Code
///
/// 获得中文星期名称
///
///
public static string GetCnWeek()
{
switch (DateTime.Now.DayOfWeek)
{
case DayOfWeek.Monday:
return "星期一";
case DayOfWeek.Tuesday:
return "星期二";
case DayOfWeek.Wednesday:
return "星期三";
case DayOfWeek.Thursday:
return "星期四";
case DayOfWeek.Friday:
return "星期五";
case DayOfWeek.Saturday:
return "星期六";
case DayOfWeek.Sunday:
return "星期天";
default:
return "";
}
}
///
/// 获得中文星期名称
///
///
public static string GetCnWeek()
{
switch (DateTime.Now.DayOfWeek)
{
case DayOfWeek.Monday:
return "星期一";
case DayOfWeek.Tuesday:
return "星期二";
case DayOfWeek.Wednesday:
return "星期三";
case DayOfWeek.Thursday:
return "星期四";
case DayOfWeek.Friday:
return "星期五";
case DayOfWeek.Saturday:
return "星期六";
case DayOfWeek.Sunday:
return "星期天";
default:
return "";
}
}
以下是变种:
Code
//方法2:摘自http://www.cnblogs.com/perfect/archive/2006/11/25/571674.html
//必须添加 using System.Collections
private void btnQuery_DayOfWeek_Click(object sender, EventArgs e)
{
Hashtable htb = new Hashtable();
htb.Add("Monday","星期一");
htb.Add("Tuesday", "星期二");
htb.Add("Wednesday", "星期三");
htb.Add("Thursday", "星期四");
htb.Add("Friday", "星期五");
htb.Add("Saturday", "星期六");
htb.Add("Sunday", "星期日");
string str = htb[DateTime.Now.DayOfWeek.ToString().Trim()].ToString();
MessageBox.Show("今天是:" + str);
}
//方法2:摘自http://www.cnblogs.com/perfect/archive/2006/11/25/571674.html
//必须添加 using System.Collections
private void btnQuery_DayOfWeek_Click(object sender, EventArgs e)
{
Hashtable htb = new Hashtable();
htb.Add("Monday","星期一");
htb.Add("Tuesday", "星期二");
htb.Add("Wednesday", "星期三");
htb.Add("Thursday", "星期四");
htb.Add("Friday", "星期五");
htb.Add("Saturday", "星期六");
htb.Add("Sunday", "星期日");
string str = htb[DateTime.Now.DayOfWeek.ToString().Trim()].ToString();
MessageBox.Show("今天是:" + str);
}
第二种:实质和第一种没多大差别,不过代码看起来少几行而已。依赖于微软DayOfWeek这Enum的定义。
string week = DateTime.Today.ToLongDateString()+" "+"星期"+"日一二三四五六".Substring((int)System.DateTime.Now.DayOfWeek, 1);
//以下来自:http://www.cnblogs.com/sjcatsoft/archive/2005/10/02/248094.html
Code
public static string CnWeekDayName(DateTime dt)
{
string[] arrCnNames = new string[]{"日", "一", "二", "三", "四", "五", "六"};
return "星期" + arrCnNames[(int)dt.DayOfWeek];
}
public static string CnWeekDayName(DateTime dt)
{
string[] arrCnNames = new string[]{"日", "一", "二", "三", "四", "五", "六"};
return "星期" + arrCnNames[(int)dt.DayOfWeek];
}
//以下来自http://www.cnblogs.com/lanting/archive/2006/04/19/379102.html
public enum WeekDay
{
星期天,
星期一,
星期二,
星期三,
星期四,
星期五,
星期六
}
调用时{
星期天,
星期一,
星期二,
星期三,
星期四,
星期五,
星期六
}
((WeekDay)DateTime.Today.DayOfWeek).ToString()
第三种:.NET自带的System.Globalization.CultureInfo,
string week=System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) ;
//以下指定中国大陆
DateTime.Now.ToString("ddd",new System.Globalization.CultureInfo("zh-cn"));
string week = DateTime.Today.ToString("yyyy年MM月dd日 ddd", new System.Globalization.CultureInfo("zh-cn"));
string week = DateTime.Today.ToString("yyyy年MM月dd日 ddd", new System.Globalization.CultureInfo("zh-cn"));