急,在线等,回答正确,立马给分!-如何取一年中的当前周数

木棉花大榕树 2005-04-14 09:17:57
问:用那个控件或者不用控件也行,取得当前时间的周数,比如说当前是一年中的第2周,则返回数字是2,如何实现,多谢!
...全文
176 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
木棉花大榕树 2005-04-16
  • 打赏
  • 举报
回复
多谢各位了,辛苦各位了!
samchoy 2005-04-15
  • 打赏
  • 举报
回复
Returns the week of the year represented by a TDateTime value.

Unit
DateUtils

Category
date/time routines

extern PACKAGE Word __fastcall WeekOfTheYear(const System::TDateTime AValue);
extern PACKAGE Word __fastcall WeekOfTheYear(const System::TDateTime AValue, Word &AYear);

Description
Call WeekOfTheYear to obtain the week of the year represented by a specified TDateTime value. WeekOfTheYear returns a value between 1 and 53.

AYear returns the year in which the specified week occurs. Note that this may not be the same as the year of AValue. This is because the first week of a year is defined as the first week with four or more days in that year. This means that if the first calendar day of the year is a Friday, Saturday, or Sunday, then for the first three, two, or one days of the calendar year, WeekOfTheYear returns the last week of the previous year. Similarly, if the last calendar day of the year is a Monday, Tuesday, or Wednesday, then for the last one, two, or three days of the calendar year, WeekOfTheYear returns 1 (the first week of the next calendar year).

Note: WeekOfTheYear uses the ISO 8601 standard definition of a week. That is, a week is considered to start on a Monday and end on a Sunday.
Note: WeekOfTheYear returns the same value as the WeekOf function.
Friecin 2005-04-15
  • 打赏
  • 举报
回复
#include <DateUtils.hpp>

ShowMessage( "当前是今年的第:" + AnsiString( WeekOfTheYear(Now()) ) + "周" );
木棉花大榕树 2005-04-15
  • 打赏
  • 举报
回复
不好意思,我用的是cb5,帮助里面没发现有这个函数?要include什么文件?能否给个简单例子?
laisf 2005-04-15
  • 打赏
  • 举报
回复
想同的问题,有人刚问过了。
constantine 2005-04-15
  • 打赏
  • 举报
回复
WeekOfTheYear
没有什么好说了,楼上都有了
icwin 2005-04-15
  • 打赏
  • 举报
回复
up
yjy1001 2005-04-15
  • 打赏
  • 举报
回复
例如 2005年1月1 - 8日, BCB 会认为53周, 这个结果肯定跟楼主的需要有出入
请大家告诉我你们测试的结果
yjy1001 2005-04-15
  • 打赏
  • 举报
回复
if (WeekOfTheYear(Now()) == GetWeekOfYear())
{
ShowMessage(GetWeekOfYear());
}
else
{
ShowMessage(WeekOfTheYear(Now()));
}


可以认证出,只有偶的计算函数才是正确的, 嘿嘿
你可以从1月1日逐个逐个星期的测试
yjy1001 2005-04-15
  • 打赏
  • 举报
回复
这个简单

#include <math.h >

WORD GetWeekOfYear()
{
TDate dt_fst, dt = Now();

WORD year, month, day, week;
dt.DecodeDate(&year, &month, &day);
dt = EncodeDate(year, month, day);

dt_fst = EncodeDate(year, 1, 1);//得到当年的1月1日的时间
week = dt_fst.DayOfWeek();//得到当年的1月1日的星期数

return ceil(((dt.Val - dt_fst.Val) + week) / 7);

}
happyct 2005-04-15
  • 打赏
  • 举报
回复
为冲星,蹭点分
happyct 2005-04-15
  • 打赏
  • 举报
回复
哈哈
JetKingLau 2005-04-15
  • 打赏
  • 举报
回复
对,CB5 没有,需要自己写代码。
木棉花大榕树 2005-04-15
  • 打赏
  • 举报
回复
[C++ Error] Unit1.h(10): E2209 Unable to open include file 'DateUtils.hpp'

编译报错,cb5好像没有这个文件,:(
Friecin 2005-04-14
  • 打赏
  • 举报
回复
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *WeekName[7]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

SYSTEMTIME Now,Tmp;
GetSystemTime(&Now);

Tmp.wYear=2222; //要查询的年份
Tmp.wMonth=12; //要查询的月份
Tmp.wDay=31; //要查询的日期
Tmp.wHour=1; //要查询的小时
Tmp.wMinute=1; //要查询的分数
Tmp.wSecond=1; //要查询的秒数
Tmp.wMilliseconds=1; //要查询的微秒

if(SetSystemTime(&Tmp)!=0)
{
GetSystemTime(&Tmp);
ShowMessage(WeekName[Tmp.wDayOfWeek]);
}
else
{
ShowMessage(GetLastError());//如果错误值是87的话,是参数设置有错。
}
SetSystemTime(&Now);
}
木棉花大榕树 2005-04-14
  • 打赏
  • 举报
回复
怎么解决,可否把语句写出来呢?
ArWen 2005-04-14
  • 打赏
  • 举报
回复
这个问题很简单的.

用TDateTime即可解决

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧