当前位置:网站首页>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;
}边栏推荐
猜你喜欢

PowerBI中导出数据方法汇总

Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic

【IDEA】使用插件一键逆向生成代码

二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作

map集合赋值到数据库

Verilog and VHDL signed and unsigned number correlation operations

Always report errors when connecting to MySQL database

webauthn——官方开发文档

RPA advanced (II) uipath application practice

Summary of data export methods in powerbi
随机推荐
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
抖音海外版TikTok:正与拜登政府敲定最终数据安全协议
A white hole formed by antineutrons produced by particle accelerators
TIPC introduction 1
RPA advanced (II) uipath application practice
Multi line display and single line display of tqdm
The difference between SQL left join main table restrictions written after on and where
MySQL basic statement
基于Hardhat和Openzeppelin开发可升级合约(一)
PHP tea sales and shopping online store
Approximate sum count (approximate
Jenkins安装
C#多维数组的属性获取方法及操作注意
Is bond fund safe? Does the bond buying foundation lose principal?
sql left join 主表限制条件写在on后面和写在where后面的区别
从ros1到ros2配置的一些东西
SQLite modify column type
Skills of PLC recorder in quickly monitoring multiple PLC bits
LVM operation
bedtools使用教程