月份的计算

huangapple go评论85阅读模式
英文:

the calculation of the month

问题

  1. var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));
  2. @override
  3. void minus() {
  4. setState(() {
  5. if (defaultmonth != DateTime.now().year - 1) {
  6. defaultmonth--;
  7. }
  8. });
  9. }
  10. @override
  11. void add() {
  12. setState(() {
  13. if (defaultmonth != DateTime.now().year + 1) {
  14. defaultmonth++;
  15. }
  16. });
  17. }

我想使用这个方法来更改月份。但如果我在202301运行'mius',我希望它变成202212而不是202300。有人可以帮助我吗?

英文:
  1. var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));
  2. @override
  3. void minus() {
  4. setState(() {
  5. if (defaultmonth != DateTime.now().year - 1) {
  6. defaultmonth--;
  7. }
  8. });
  9. }
  10. @override
  11. void add() {
  12. setState(() {
  13. if (defaultmonth != DateTime.now().year + 1) {
  14. defaultmonth++;
  15. }
  16. });
  17. }

I want to change the moon using this method. But if I run 'mius' on 202301, I want it to be 202212 instead of 202300. Is there anyone who can help me?

答案1

得分: 0

Here is the translated code:

  1. import 'package:flutter/material.dart';
  2. import 'package:intl/intl.dart';
  3. class PageTestWidget extends StatefulWidget {
  4. const PageTestWidget({Key? key}) : super(key: key);
  5. @override
  6. State<PageTestWidget> createState() => _PageTestWidgetState();
  7. }
  8. class _PageTestWidgetState extends State<PageTestWidget> {
  9. var date = DateTime.now();
  10. void minus() {
  11. setState(() {
  12. date = date.copyWith(month: date.month - 1);
  13. });
  14. }
  15. void add() {
  16. setState(() {
  17. date = date.copyWith(month: date.month + 1);
  18. });
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. appBar: AppBar(title: Text('Demo')),
  24. body: Column(
  25. children: [
  26. const Row(),
  27. Text(DateFormat('yyyyMM').format(date)),
  28. const SizedBox(height: 20),
  29. Row(
  30. children: [
  31. Expanded(
  32. child: TextButton(onPressed: minus, child: const Text('minus')),
  33. ),
  34. Expanded(
  35. child: TextButton(onPressed: add, child: const Text('add')),
  36. ),
  37. ],
  38. )
  39. ],
  40. ),
  41. );
  42. }
  43. }

Is there anything else you need?

英文:
  1. import &#39;package:flutter/Material.dart&#39;;
  2. import &#39;package:intl/intl.dart&#39;;
  3. class PageTestWidget extends StatefulWidget {
  4. const PageTestWidget({Key? key}) : super(key: key);
  5. @override
  6. State&lt;PageTestWidget&gt; createState() =&gt; _PageTestWidgetState();
  7. }
  8. class _PageTestWidgetState extends State&lt;PageTestWidget&gt; {
  9. var date = DateTime.now();
  10. void minus() {
  11. setState(() {
  12. date = date.copyWith(month: date.month - 1);
  13. });
  14. }
  15. void add() {
  16. setState(() {
  17. date = date.copyWith(month: date.month + 1);
  18. });
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. appBar: AppBar(title: Text(&#39;Demo&#39;)),
  24. body: Column(
  25. children: [
  26. const Row(),
  27. Text(DateFormat(&#39;yyyyMM&#39;).format(date)),
  28. const SizedBox(height: 20),
  29. Row(
  30. children: [
  31. Expanded(
  32. child:
  33. TextButton(onPressed: minus, child: const Text(&#39;minus&#39;))),
  34. Expanded(
  35. child: TextButton(onPressed: add, child: const Text(&#39;add&#39;))),
  36. ],
  37. )
  38. ],
  39. ),
  40. );
  41. }
  42. }

答案2

得分: 0

我猜你让你的代码变得复杂了:

  1. var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));
  2. @override
  3. void minus() {
  4. setState(() {
  5. if (defaultmonth != DateTime.now().year - 1) {
  6. defaultmonth--;
  7. }
  8. });
  9. }
  10. @override
  11. void add() {
  12. setState(() {
  13. if (defaultmonth != DateTime.now().year + 1) {
  14. defaultmonth++;
  15. }
  16. });
  17. }

通常,月份的计算与普通的整数计算不同,所以不要使用整数进行计算,而是使用DateTime对象进行计算。

示例:

  1. DateTime defaultmonth = DateTime.now();
  2. var finalDefaultmonth;
  3. @override
  4. void minus() {
  5. setState(() {
  6. if (defaultmonth.year != DateTime.now().year - 1 || defaultmonth.month != 1) {
  7. DateTime newDateTime = DateTime(defaultmonth.year, defaultmonth.month - 1);
  8. finalDefaultmonth = DateFormat('yyyyMM').format(newDateTime);
  9. }
  10. });
  11. }
英文:

I guess you made your code complicated:

  1. var defaultmonth = int.parse(DateFormat(&#39;yyyyMM&#39;).format(DateTime.now()));
  2. @override
  3. void minus() {
  4. setState(() {
  5. if (defaultmonth != DateTime.now().year - 1) {
  6. defaultmonth--;
  7. }
  8. });
  9. }
  10. @override
  11. void add() {
  12. setState(() {
  13. if (defaultmonth != DateTime.now().year + 1) {
  14. defaultmonth++;
  15. }
  16. });
  17. }

Generally the month calculations are different from normal integer calculations so Instead of making calculations from integers you do the calculations on DateTime objects.

Example:

  1. DateTime defaultmonth = DateTime.now();
  2. var finalDefaultmonth;
  3. @override
  4. void minus() {
  5. setState(() {
  6. if (defaultmonth.year != DateTime.now().year - 1 || currentDateTime.month != 1) {
  7. DateTime newDateTime = DateTime(currentDateTime.year, currentDateTime.month - 1);
  8. finalDefaultmonth = DateFormat(&#39;yyyyMM&#39;).format(newDateTime);
  9. }
  10. });
  11. }

huangapple
  • 本文由 发表于 2023年5月29日 23:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定