/// <summary>
/// 取得期貨交易月份
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public string GetFutureTradeMonth(string type)
{
string yearMonth = "";
DateTime nowDate = DateTime.Now;
// 目前年月
int year = Convert.ToInt32(nowDate.Year.ToString());
int month = nowDate.Month;
//取得月份到期日(當月第3個星期三)
// 本月第一天
DateTime monthExp = new DateTime(nowDate.Year, nowDate.Month, 1, 14, 0, 0);
// 找到第一個星期三
while (monthExp.DayOfWeek != DayOfWeek.Wednesday)
monthExp = monthExp.AddDays(1);
monthExp = monthExp.AddDays(14);
if (type == "NearMonth1" || type == "NearMonth2" || type == "NearMonth3" || type == "NearSeason1" || type == "NearSeason2" || type == "NearSeason3")
{
//連續近月1
if (nowDate > monthExp)
{
month += 1;
if (month == 13)
{
month = 1;
year = year + 1;
}
}
}
if (type == "NearMonth2" || type == "NearMonth3" || type == "NearSeason1" || type == "NearSeason2" || type == "NearSeason3")
{
//連續近月2
month += 1;
if (month == 13)
{
month = 1;
year = year + 1;
}
}
if (type == "NearMonth3" || type == "NearSeason1" || type == "NearSeason2" || type == "NearSeason3")
{
//連續近月3
month += 1;
if (month == 13)
{
month = 1;
year = year + 1;
}
}
if (type == "NearSeason1" || type == "NearSeason2" || type == "NearSeason3")
{
//接續季月1
// 檢查連續近月3是否剛好為季月
if (month == 3 || month == 6 || month == 9 || month == 12)
{
month = month + 3;
if (month > 12)
{
month = 3;
year = year + 1;
}
}
else if (month == 1 || month == 2)
{
month = 3;
}
else if (month == 4 || month == 5)
{
month = 6;
}
else if (month == 7 || month == 8)
{
month = 9;
}
else if (month == 10 || month == 11)
{
month = 12;
}
}
if (type == "NearSeason2" || type == "NearSeason3")
{
//接續季月2
month = month + 3;
if (month > 12)
{
month = 3;
year = year + 1;
}
}
if (type == "NearSeason3")
{
//接續季月3
month = month + 3;
if (month > 12)
{
month = 3;
year = year + 1;
}
}
// 組合年月
yearMonth = year + "-" + month.ToString("00");
return yearMonth;
}