当前位置:网站首页>《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;
}
边栏推荐
- Paddleserving服务化部署 tensorrt报错, shape of trt subgraph is [-1,-1,768],
- How did "leek" give money to "sickle"? 2020-03-07
- History and value of forked coins | eth, BCH, BSV 2020-03-08
- 49、Mysql使用
- From the casino logic, analyze the investment value of platform currency 2020-03-03
- 總結的太好了!終於有人把SQL的各種連接Join都講明白了
- 剑指 Offer 42. 连续子数组的最大和-动态规划法
- US pressure surges, tiktok changes global safety director
- SPARK闲杂--为什么复用Exchange和subquery
- mySQL 2502 2503错误
猜你喜欢

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

从 B 站崩溃报告看分布式系统的技术栈

Redis message subscription

60、wsgiref手写web框架+jinja2模块初识

Interview question: outer margin folding problem (bug of block level elements in ordinary document flow)

By voting for the destruction of STI by Dao, seektiger is truly community driven

一款关于日常习惯打卡的小程序

1、决策树

Complete square number

OpenCV极坐标转换函数warpPolar的使用
随机推荐
Application of SCA on devsecops platform
46、IO模型
Interview question: outer margin folding problem (bug of block level elements in ordinary document flow)
Gateway新一代网关
visual studio 2022(VS 2022)无法读取内存的问题
5G正当时,无人驾驶未来将驶向何方?
leetcode:287. Find the repetition number [fast and slow pointer board]
[C classes and objects] - Methods and class and object programming in C
【flask入门系列】请求钩子与上下文
一款关于日常习惯打卡的小程序
[C # console] - C # console class
DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)
Super dry! Thoroughly understand golang memory management and garbage collection
By voting for the destruction of STI by Dao, seektiger is truly community driven
Yolov5 label and establish your own data set
Textview text up and down
在VSCode中设置settings.json
警惕!又一起网络钓鱼攻击事件:Uniswap被盗810万美元
[kernel] character device that drives development and learning
ObjectARX--自定义圆的实现