当前位置:网站首页>《Flutter入门》flutter计算最近1个月、3个月、半年、12个月
《Flutter入门》flutter计算最近1个月、3个月、半年、12个月
2022-07-17 07:27:00 【Jda_wz】
产品需求:获取最近1个月、3个月、半年、1年的交易记录。
最快方式:直接用个30天、60天、180天、365天的Duration,用DateTime相见即可。
存在问题:实际上是不准确的,例如3月1号的最近1个月,应该是2月1-3月1,但是2月如果只有28天,会算成1月30号开始;
优化目标:
- 往前X个月,需要根据前X个月每个月天数相加为准,不是一成不变的30*n;
- 如往前X个月没有当前天则返回前x月+1个月的1号(例如3月30往前1个月,没有2月30,则返回3月1号)
百度了下没有现成的,自己写了下,给大家参考参考吧。上代码:
///计算某个时间往前X个月的具体日期
///beforeMonth 月数 例如1、3、5、12(1年)、36(3年)
///anchorDateTime 锚定日期,比如2022-7-6
///如往前X个月没有有当前天则返回前x月+1个月的1号(例如3月30往前1个月,没有2月30,则返回3月1号)
static int getBeforeMonthTotalDays(int beforeMonth, DateTime anchorDateTime) {
DateTime startTime;
//1--先计算目标年月
if (anchorDateTime.month <= beforeMonth) {//月份不足,计算年份
int year = anchorDateTime.year - beforeMonth ~/ 12; //计算年数
int month =beforeMonth ~/ 12 * 12 + anchorDateTime.month - beforeMonth; //计算月份
if (month == 0) {//0月改为上一年的12月
year -=1;
month = 12;
}
startTime = DateTime(year, month);
} else {//月份足,直接计算月份
startTime =
DateTime(anchorDateTime.year, anchorDateTime.month - beforeMonth);
}
//2--计算那个月有多少天,根据该1号和次月1号相差得出
int totalDays = 0;
totalDays = startTime.month == 12
? DateTime(startTime.year + 1, 1, 1).difference(startTime).inDays
: DateTime(startTime.year, startTime.month + 1, 1)
.difference(startTime)
.inDays;
//3--计算目标日,根据目标月是否含有目标日,如果没有则返回次月1号
if (totalDays >= anchorDateTime.day) {// print("该月天数有当前天,直接返回那天");
//开始日期那个月有那一天
startTime = DateTime(startTime.year, startTime.month, anchorDateTime.day);
} else { // print("该月天数没有当前天,返回该月次月1号");
//没有那一天,则变成次月1号
if (startTime.month == 12) {//12月,改成次年1月1日
startTime = DateTime(startTime.year + 1, 1, 1);
} else { //其他月份直接+1
startTime = DateTime(startTime.year, startTime.month + 1, 1);
}
}
print(
"$anchorDateTime 的最近$beforeMonth个月的开始时间为$startTime,总间隔天数${anchorDateTime.difference(startTime).inDays}");
return anchorDateTime.difference(startTime).inDays;
}
边栏推荐
- Sword finger offer 42 Maximum sum dynamic programming method for continuous subarrays
- Viewing the technology stack of distributed system from the crash report of station B
- 数组习题三
- Use of OpenCV polar transformation function warppolar
- Excellent résumé! Enfin quelqu'un a compris toutes les connexions SQL
- Error received from peer ipv4/Connection reset by peer Paddleserving服务化部署后报错
- ObjectARX--自定义圆的实现
- The core problem of concurrent programming
- How did "leek" give money to "sickle"? 2020-03-07
- Gateway新一代网关
猜你喜欢

Classic general pbootcms flower website template source code, adaptive mobile terminal, with background management

凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节

With this "programmer code interview guide" from Zuo Chengyun (Zuo Shen), I joined byte

微服务与微服务架构

Database review -- database recovery technology

How does the V8 engine recycle garbage memory?

全志V3s学习记录(13)OV2640的使用

Redis的发布和订阅

经典通用的Pbootcms花卉网站模板源码,自适应手机端,带后台管理

DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)
随机推荐
QT related problems encountered when writing code
才意识到自己“奇葩”的360,会不会有些晚?
Redis data persistence
Dependency injection method
Leetcode daily question 2021/7/11-2021/7/17
Error received from peer ipv4/Connection reset by peer Paddleserving服务化部署后报错
Redis distributed lock
Understanding of fast and slow pointer
Redis的发布和订阅
Redis新数据类型——Bitmaps
Interview question: outer margin folding problem (bug of block level elements in ordinary document flow)
Bean、
STM32F103C8T6硬件IIC控制4针0.96寸OLED显示屏
5.2 database security
Redis6新数据类型——HyperLogLog
Yolov5 label and establish your own data set
依赖注入方式
深度学习之 7 深度前馈网络2
Eureka自我保护
全志V3s学习记录(13)OV2640的使用