第几周计算代码和C#函数方法
周计算方法C#
/// 当前月有多少天
/// </summary>
/// <param name="y"></param>
/// <param name="m"></param>
/// <returns></returns>
public static int HowMonthDay(int y, int m)
{
int mnext;
int ynext;
if (m < 12)
{
mnext = m + 1;
ynext = y;
}
else
{
mnext = 1;
ynext = y + 1;
}
DateTime dt1 = System.Convert.ToDateTime(y + "-" + m + "-1");
DateTime dt2 = System.Convert.ToDateTime(ynext + "-" + mnext + "-1");
TimeSpan diff = dt2 - dt1;
return diff.Days;
}/**//// <summary>
/// 得到一年中的某周的起始日和截止日
/// 年 nYear
/// 周数 nNumWeek
/// 周始 out dtWeekStart
/// 周终 out dtWeekeEnd
/// </summary>
/// <param name="nYear"></param>
/// <param name="nNumWeek"></param>
/// <param name="dtWeekStart"></param>
/// <param name="dtWeekeEnd"></param>
public static void GetWeek(int nYear, int nNumWeek, out DateTime dtWeekStart, out DateTime dtWeekeEnd)
{
DateTime dt = new DateTime(nYear, 1, 1);
dt = dt + new TimeSpan((nNumWeek - 1) * 7, 0, 0, 0);
dtWeekStart = dt.AddDays(-(int)dt.DayOfWeek + (int)DayOfWeek.Monday);
dtWeekeEnd = dt.AddDays((int)DayOfWeek.Saturday - (int)dt.DayOfWeek + 1);
}/**//// <summary>
/// 求某年有多少周
/// 返回 int
/// </summary>
/// <param name="strYear"></param>
/// <returns>int</returns>
public static int GetYearWeekCount(int strYear)
{
string returnStr = "";System.DateTime fDt = DateTime.Parse(strYear.ToString() + "-01-01");
int k = Convert.ToInt32(fDt.DayOfWeek);//得到该年的第一天是周几
if (k == 1)
{
int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
int countWeek = countDay / 7 + 1;
return countWeek;}
else
{
int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
int countWeek = countDay / 7 + 2;
return countWeek;
}}
/**//// <summary>
/// 求当前日期是一年的中第几周
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int WeekOfYear(DateTime curDay)
{
int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "- " + "1-1 ").DayOfWeek);int days = curDay.DayOfYear;
int daysOutOneWeek = days - (7 - firstdayofweek);if (daysOutOneWeek <= 0)
{
return 1;
}
else
{
int weeks = daysOutOneWeek / 7;
if (daysOutOneWeek % 7 != 0)
weeks++;return weeks + 1;
}
}
=========================================================================
C 语言中求某一天是日历上第几周怎么计算?
用c语言编程,只知道年月日,星期几,那么怎么计算某一天是第几周呢?
添加评论 分享
按票数排序 2 个回答赞同
1
反对,不会显示你的姓名
skydiver,C++ is a terrible language.
文闻 赞同
参考 Stackoverflow:c++ - How do I calculate the week number given a date?
使用 C 标准库 time.h 就可以完成这个计算。
示例代码如下(来自上面链接)
#include <stdio.h>
#include <string.h>
#include <time.h>int
main(void)
{
struct tm tm;
char timebuf[64];// Zero out struct tm
memset(&tm, 0, sizeof(struct tm));// November 4, 2008 11:00 pm
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 23;
tm.tm_mday = 4;
tm.tm_mon = 10;
tm.tm_year = 108;// Call mktime to recompute tm.tm_wday and tm.tm_yday
mktime(&tm);if (strftime(timebuf, sizeof(timebuf), "%W", &tm) != 0) {
printf("Week number is: %s\n", timebuf);
}return 0;
}
2013-05-27 添加评论 感谢 分享 收藏 • 没有帮助 • 举报赞同
1
反对,不会显示你的姓名
汪建,茫茫人海一小研
lena zhao 赞同
1、知道年月日,可以计算这个日期是这一年的第几天m,也就是减去1月1日,例如:2月2日是这一年的第33天
2、将得到的天数m除以7,取整,得到过去了多少周n
3、将得到的天数m对7取余,与给定日期是星期几做对比,可以判断出是否出现了跨周的情况,如果出现了,再将2中得到的n加1