【发布时间】:2011-08-15 17:30:15
【问题描述】:
我想做一个 if 语句 if month == january 例如加载一些东西 else if month == april 加载其他内容。 有人可以帮忙吗 谢谢
【问题讨论】:
标签: c#
我想做一个 if 语句 if month == january 例如加载一些东西 else if month == april 加载其他内容。 有人可以帮忙吗 谢谢
【问题讨论】:
标签: c#
为 Month 做一个枚举并使用这个 case 语句
switch (DateTime.Now.Month)
{
case MonthEnum.JAN
break;
}
【讨论】:
你可以使用 DateTime.Now.Month
if(DateTime.Now.Month == 1)
{
//January
}
else if (DateTime.Now.Month == 2)
{
//February
}
//etc
【讨论】:
您可以像这样使用一年中的月份创建一个枚举:
public enum Months{
January = 1,
February = 2,
.
.
December = 12
}
然后试试
if(Datetime.Now.Month == (int)Months.January){
//Do Something...
} else if(Datetime.Now.Month == (int)Months.April){
//Do Something else
}
希望这会有所帮助。问候,
胡安·阿尔瓦雷斯
【讨论】:
string monthName = DateTime.Now.ToString("MMMM");
if(monthName == "april"{
...
}
【讨论】:
switch (DateTime.Now.Month)
{
case 1: // JAN
...
break;
case 2: // FEB
...
break;
}
【讨论】:
使用DateTime.Month 属性检查月份
switch (DateTime.Now.Month){
case 1://January stuff here
break;
case 2://Feb stuff here etc...
}
【讨论】:
见Is there a predefined enumeration for Month in the .NET library?。
string monthName = CultureInfo.CurrentCulture.DateTimeFormat
.GetMonth ( DateTime.Now.Month );
switch (monthName)
{
case "January":
// do something
break;
case "April":
// do something
break;
// etc.
}
【讨论】:
几种方法
if(DateTime.Today.Month == 1){
// do something
}
if(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month) == "January"){
// do something
}
对于第二种方法,您需要包含 System.Globalization
【讨论】:
你可以使用Month属性,范围是1-12:
int month = DateTime.Now.Month;
if(month == 4) //April
{..
【讨论】:
var now = DateTime.Now;
var monthName = now.ToString("MMMM")
if (monthName == "January)
{
//load something
}
else if (monthName == "April")
{
//load something else.
}
【讨论】: