您的位置:首页 > 其它

开学时间为2009年2月18日,计算当前日期是第几周

2013-03-28 19:57 295 查看
加一label1,用来显示第几周,在加一个button,点击表示计算

代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private string s = "2009-2-18";

private int WeekOfYear(string date)

{

DateTime curDay = Convert.ToDateTime(date);

DateTime oldDay = Convert.ToDateTime(s);

int days1 = oldDay.DayOfYear;//获取开学时间离元旦的天数

int days2= curDay.DayOfYear;//获取目前时间离元旦的天数

int days = days2 - days1;

label3.Text = days.ToString();

int daysOutOneWeek = days;

if (daysOutOneWeek <= 0)

{

return 1;

}

else

{

int weeks = daysOutOneWeek / 7;

if (daysOutOneWeek % 7 != 0)

weeks++;

return weeks + 1;

}

}

private void button1_Click(object sender, EventArgs e)

{

string t;

int m;

t = "2009-2-22";

m=WeekOfYear(t);

label1.Text = m.ToString();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: