public ActionResult Cnlender()
{
var date = DateTime.Now;
StringBuilder result = DayTime(date.Year, date.Month);
return View(result);
}
public ActionResult WeekCnlender()
{
var date = DateTime.Now;
StringBuilder result = WeekTime(date.Year, date.Month);
return View(result);
}
public JsonResult Cnlenders(int Year, int Month, int TypeID)
{
StringBuilder result = new StringBuilder();
if(TypeID == 1)
result = DayTime(Year, Month);
if(TypeID == 2)
result = WeekTime(Year, Month);
return Json(result.ToString());
}
#region //日历 //周历
protected StringBuilder DayTime(int? Year, int? Month)
{
var result = new StringBuilder();
result.AppendFormat("
+ "
+ "
星期日 | "星期一 | 星期二 | 星期三 | 星期四 | 星期五 | "星期六 |
"); | " + (x - day) + " | ");
return result;
}
protected StringBuilder WeekTime(int? Year, int? Month)
{
var result = new StringBuilder();
int year = Year == null ? DateTime.Now.Year : (int)Year;
int month = Month == null ? DateTime.Now.Month : (int)Month;
var date = DateTime.Parse(string.Format("{0}-{1}", year, month));
int Days = DateTime.DaysInMonth(date.Year, date.Month);//获取该月天数
int day = (int)date.AddDays(1 - date.Day).DayOfWeek;//获取该月第一天的星期
double wek = (double)Days / 7;
int weeks = (int)Math.Ceiling(wek);
if (!DateTime.IsLeapYear(date.Year))
{
if (date.Month == 2)//如果是闰年2月
weeks += 1;
}
result.AppendFormat("
for (int i = 1; i <= weeks; i++)
{
result.AppendFormat(""
+ date.AddDays(1 - date.Day - day + i + (i - 1) * 6).ToString("MM-dd")
+ " 至 "
+ date.AddDays(1 - date.Day - day + i + i * 6).ToString("MM-dd")
+ "
");
}
result.AppendFormat("
return result;
}
#endregion
js
function ShowDate() {
var isdate = new Date();
var year = isdate.getFullYear(); //获取当前年
var month = isdate.getMonth() + 1; //获取当前月
//年份下拉框
jQuery("#time").append("年");
var years = 2012
for (var i = 0; i < 20; i++) {
if (years == year)
jQuery("#year").append("");
else
jQuery("#year").append("");
years++;
}
//月份下拉框
jQuery("#time").append("月");
for (var i = 1; i < 13; i++) {
if (i == month)
jQuery("#month").append("");
else
jQuery("#month").append("");
}
}
function ChangeDate() {
var year = jQuery("#year").val();
var month = jQuery("#month").val();
var typeid = jQuery("#typeid").val();
jQuery.ajax({
url: "/Reading/Cnlenders",
type: "POST",
data: { Year: year, Month: month, TypeID:typeid },
success: function (msg) {
jQuery(msg).replaceAll("#Calendar");
}
});
}
日历图:
中间可以加入用户的互动等等
周历 只是计算出了时间没搞样式