基于当月的硬编码月份和年份

时间:2012-11-08 06:34:10

标签: c# asp.net

根据当前月份和年份登录后我有一个登录页面我必须显示一个标签,如果月份是十月,它应该显示为2012年7月至12月,如果是1月,它应该看起来像2013年1月 - 6月如何我能做到吗?

3 个答案:

答案 0 :(得分:2)

如果您能以某种方式得到当前日期(我会说,给定),只需编写类似下面的伪代码的代码:

today = now()                              // Current date
if today.month() < 6:                      // Assumes months 0-based.
    output = "January-June " + today.year()
else:
    output = "July-December " + today.year()

答案 1 :(得分:2)

这是你的aspx代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="datelabel" runat="server" />

    </div>
    </form>
</body>
</html>

这是你的c#代码:

 protected void Page_Load(object sender, EventArgs e)
        {

            DateTime date = DateTime.Now;    // display format: 4/25/2008 11:45:44 AM
            int mon = date.Month;
            if (mon < 6)
            {
                datelabel.Text = "Jan-june" +date.Year;
            }
            else
                datelabel.Text = "july-dec" +date.Year;

        }

答案 2 :(得分:0)

试试这个,

        var today = DateTime.Now;
        var result = today.Month <= 6
                         ? string.Format("January-June {0}", today.AddYears(1).Year)
                         : string.Format("July-December  {0}", today.Year);

结果:2012年7月至12月

相关问题