twzheng's cppblog

『站在风口浪尖紧握住鼠标旋转!』 http://www.cnblogs.com/twzheng

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  136 随笔 :: 78 文章 :: 353 评论 :: 0 Trackbacks
 C# 怎么显示中文格式的日期、星期几


//该语句显示的为英文格式
DateTime.Now.DayOfWeek.ToString();

//显示中文格式星期几
 
"星期" + DateTime.Now.ToString("ddd",new System.Globalization.CultureInfo("zh-cn"));   


//显示中文格式的日期
DateTime.Now.ToLongDateString();   // 显示格式为"2008年1月1日"
DateTime.Now.ToString("yyyy年MM月dd日");   // 显示格式为"2008年01月01日",注意:格式字符串中的字母大小写不能错
posted on 2007-12-01 17:20 谭文政 阅读(9773) 评论(5)  编辑 收藏 引用 所属分类: 基础知识

评论

# re: C# 怎么显示中文格式的日期、星期几 2008-01-14 20:06 zmsky
//显示中文格式星期几
DateTime.Now.ToString("dddd",new System.Globalization.CultureInfo("zh-cn"));   回复  更多评论
  

# re: C# 怎么显示中文格式的日期、星期几 2008-01-14 20:09 zmsky
//更新简捷的显示中文格式星期几用4个dddd就可以搞定了,不需任何拼凑
DateTime.Now.ToString("dddd",new System.Globalization.CultureInfo("zh-cn"));  回复  更多评论
  

# re: C# 怎么显示中文格式的日期、星期几 2008-09-04 20:58 金刚
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace TranWeek
{
public partial class Form1 : Form
{
private static string[] Eweek ={ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
private static string[] Cweek ={ "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日", };

public Form1()
{
InitializeComponent();
this.button1.Click += new EventHandler(button_Click);
this.button2.Click += new EventHandler(button_Click);
this.button3.Click += new EventHandler(button_Click);
}

private void Form1_Load(object sender, EventArgs e)
{

}

/// <summary>
/// 根据英文星期名称获得中文星期名称
/// </summary>
/// <param name="eweek"></param>
/// <returns></returns>
private string GetCWeek1(string eweek)
{


string cweek = "";
for (int i = 0; i < Eweek.Length; i++)
{
if (Eweek[i].ToUpper() != eweek.ToUpper())
{ continue; }
else
{
cweek = Cweek[i];
break;
}
}
return cweek;
}
/// <summary>
/// 写义一个结构,此结构两个成员分别存储中英文星期的字符
/// </summary>
public struct CSWeak
{
public string Eweek;
public string Cweek;
}
/// <summary>
/// 根据英文星期名称获得中文星期名称2
/// </summary>
/// <param name="eweek"></param>
/// <returns></returns>
private string GetCWeek2(string eweek)
{
string ret = "";
CSWeak[] DesWeek = new CSWeak[7];
CSWeak sk = new CSWeak();
for (int i = 0; i < 7; i++)
{
sk.Eweek = Eweek[i];
sk.Cweek = Cweek[i];
DesWeek[i] = sk;
}
for (int i = 0; i < DesWeek.Length; i++)
{
if (eweek.ToUpper() != DesWeek[i].Eweek.ToUpper())
{
ret = "";
continue;
}
else
{
ret = DesWeek[i].Cweek;
break;
}
}
return ret;
}
/// <summary>
/// 用哈希表来实现中英文星期转换
/// </summary>
/// <param name="eweek"></param>
/// <returns></returns>
private string GetCWeek3(string eweek)
{
Hashtable HT = new Hashtable(); //创建哈希表
for (int i = 0; i < Eweek.Length; i++)
{ HT.Add(Eweek[i], Cweek[i]); } //给哈希表赋值
if (HT.Contains(eweek)) //判断表中是否有指定键的项
return (string)HT[eweek]; //返回键值
else
return "";
}

private void button_Click(object sender, EventArgs e)
{
Button tempBtn = (Button)sender;
string btnName = tempBtn.Name;
string engweekname=this.dateTimePicker1.Value.DayOfWeek.ToString();
switch (btnName)
{
case "button1":
this.label1.Text = "星期:" + GetCWeek1(engweekname);
break;
case "button2":
this.label1.Text = "星期:" + GetCWeek2(engweekname);
break;
case "button3":
this.label1.Text = "星期:" + GetCWeek3(engweekname);
break;
default:
break;

}
}
}
}  回复  更多评论
  

# re: C# 怎么显示中文格式的日期、星期几 2008-10-07 16:59 czq
VS2005后显示星期的新方法是
"星期"+DateTime.Now.DayOfWeek.ToString(("d"))  回复  更多评论
  

# re: C# 怎么显示中文格式的日期、星期几 2010-06-29 13:17 飞鸽传书
确实不错,谢谢了。  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理