大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > ASP.NET技巧 > ASP.NET获取当前日期是星期几

ASP.NET获取当前日期是星期几

关键词:ASP.NET星期  阅读(608) 赞(35)

[摘要]本示例使用DateTime类的DayofWeek属性,来获取当前日期是星期几,并将返回指定日期是星期几的英文单词转化为相应的汉字。

示例:

获取当前日期是星期几

本示例使用DateTime类的DayofWeek属性,来获取当前日期是星期几,并将返回指定日期是星期几的英文单词转化为相应的汉字,运行结果如图1所示。

图1 获取当前日期是星期几

程序代码如下:

public static string dt;
public static string week;
protected void Button1_Click(object sender, EventArgs e)
{
//获取当前日期是星期几
dt = DateTime.Today.DayOfWeek.ToString();
//根据取得的星期英文单词返回汉字
switch (dt)
{ 
case "Monday":
week ="星期一";
break ;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thursday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
//利用TextBox文本框显示当前日为星期几
TextBox1.Text = week;
}

程序完整代码如下:

★★★★★Default.aspx.cs代码文件的代码★★★★★

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
public static string dt;
public static string week;
protected void Button1_Click(object sender, EventArgs e)
{
//获取当前日期是星期几
dt = DateTime.Today.DayOfWeek.ToString();
//根据取得的星期英文单词返回汉字
switch (dt)
{ 
case "Monday":
week ="星期一";
break ;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thursday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
//利用TextBox文本框显示当前日为星期几
TextBox1.Text = week;
}
}

★★★★★Default.aspx设计文件的代码★★★★★

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="显示" /></div>
</form>
</body>
</html>



相关评论