当前位置:网站首页>QT获取某个日期是第几周
QT获取某个日期是第几周
2022-07-02 09:38:00 【zjl_yujizhong】
因上一版函数测试出现和1月1日同一个周一(1-7),时会出现小一周的情况,如2022年1月8号被识别成第一周。
新改成以下方式:
ceil((start.daysTo(value) - count) /7.0) + 1;
不足两位的补0处理,确保返回值为四位的周别格式。
QString class::getWeek(QDateTime value)
{
int result = -1;
QDateTime start = QDateTime::fromString(value.toString("yyyy") + "0101", "yyyyMMdd");
int myWeek = start.date().dayOfWeek();
int count = 7 - myWeek; // 差值超过该值为下一周
double num = start.daysTo(value);
result = ceil((start.daysTo(value) - count) /7.0) + 1;
QString str = "";
if(result < 10)
str = "0" + QString::number(result);
else
str = result;
str = value.toString("yy") + str;
return str;
}---------------------------------------------------------旧版分割线---------------------------------------------------------
基于QT的QDateTime、QDate函数获取周几和日期间隔(闰年等计算由QT调用库调整返回)
获取第几周的函数:
// 计算传入时间是当年第几周
int class::getWeek(QDateTime value)
{
int result = -1; // 返回结果
// 获取当年第一天,用于判定第一周有几天
QDateTime start = QDateTime::fromString(value.toString("yyyy") + "0101", "yyyyMMdd");
int myWeek = start.date().dayOfWeek(); // dayOfWeek为获取周几(1~7)
int count = 7 - myWeek; // 第一周包含count+1天
// daysTo为返回调用日期到参数日期的天数(参数日期早于调用日期则返回负数)
double num = start.daysTo(value)/7.0;
result = ceil(num); // ceil为向上取整函数
// 因第一周大多不满7天,所以下一周需判定第一周的天数(取余)
if((start.daysTo(value) % 7) - count > 0)
++result;
return result;
}边栏推荐
- Jinshanyun - 2023 Summer Internship
- Webauthn - official development document
- STM32 single chip microcomputer programming learning
- PHP tea sales and shopping online store
- MySQL比较运算符IN问题求解
- js中给数组添加元素的方法有哪些
- C# 文件与文件夹操作
- TIPC Service and Topology Tracking4
- Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'
- Internship report skywalking distributed link tracking?
猜你喜欢

MySQL比较运算符IN问题求解

可升级合约的原理-DelegateCall

TDSQL|就业难?腾讯云数据库微认证来帮你

Develop scalable contracts based on hardhat and openzeppelin (II)

Pit of the start attribute of enumrate

C#多维数组的属性获取方法及操作注意

PKG package manager usage instance in FreeBSD

Principle of scalable contract delegatecall

MySQL comparison operator in problem solving

Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
随机推荐
Precautions for scalable contract solution based on openzeppelin
启牛商学院给的股票账户安全吗?能开户吗?
Is the stock account given by qiniu business school safe? Can I open an account?
【云原生】2.5 Kubernetes 核心实战(下)
Attribute acquisition method and operation notes of C # multidimensional array
Complement (Mathematical Simulation
TIPC Cluster5
QT learning diary 8 - resource file addition
How does the whole network display IP ownership?
Is the Ren domain name valuable? Is it worth investing? What is the application scope of Ren domain name?
Never forget, there will be echoes | hanging mirror sincerely invites you to participate in the opensca user award research
制造业数字化转型和精益生产什么关系
What are the software product management systems? Inventory of 12 best product management tools
map集合赋值到数据库
spritejs
MTK full dump抓取
Is it safe to open a stock account online? I'm a novice, please guide me
对毕业季即将踏入职场的年轻人的一点建议
Wechat applet uses Baidu API to achieve plant recognition
Programmer growth Chapter 6: how to choose a company?