月份的计算

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

the calculation of the month

问题

var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));

@override
void minus() {
  setState(() {
    if (defaultmonth != DateTime.now().year - 1) {
      defaultmonth--;
    }
  });
}

@override
void add() {
  setState(() {
    if (defaultmonth != DateTime.now().year + 1) {
      defaultmonth++;
    }
  });
}

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

英文:
var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));

@override
void minus() {
  setState(() {
      if (defaultmonth != DateTime.now().year - 1) {
        defaultmonth--;
      }
  });
}

@override
void add() {
  setState(() {
      if (defaultmonth != DateTime.now().year + 1) {
        defaultmonth++;
      }
  });
}

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:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class PageTestWidget extends StatefulWidget {
  const PageTestWidget({Key? key}) : super(key: key);

  @override
  State<PageTestWidget> createState() => _PageTestWidgetState();
}

class _PageTestWidgetState extends State<PageTestWidget> {
  var date = DateTime.now();

  void minus() {
    setState(() {
      date = date.copyWith(month: date.month - 1);
    });
  }

  void add() {
    setState(() {
      date = date.copyWith(month: date.month + 1);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Demo')),
      body: Column(
        children: [
          const Row(),
          Text(DateFormat('yyyyMM').format(date)),
          const SizedBox(height: 20),
          Row(
            children: [
              Expanded(
                child: TextButton(onPressed: minus, child: const Text('minus')),
              ),
              Expanded(
                child: TextButton(onPressed: add, child: const Text('add')),
              ),
            ],
          )
        ],
      ),
    );
  }
}

Is there anything else you need?

英文:
import &#39;package:flutter/Material.dart&#39;;
import &#39;package:intl/intl.dart&#39;;



class PageTestWidget extends StatefulWidget {
  const PageTestWidget({Key? key}) : super(key: key);

  @override
  State&lt;PageTestWidget&gt; createState() =&gt; _PageTestWidgetState();
}

class _PageTestWidgetState extends State&lt;PageTestWidget&gt; {
  var date = DateTime.now();

  void minus() {
    setState(() {
      date = date.copyWith(month: date.month - 1);
    });
  }

  void add() {
    setState(() {
      date = date.copyWith(month: date.month + 1);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  AppBar(title: Text(&#39;Demo&#39;)),
      body: Column(
        children: [
          const Row(),
          Text(DateFormat(&#39;yyyyMM&#39;).format(date)),
          const SizedBox(height: 20),
          Row(
            children: [
              Expanded(
                  child:
                      TextButton(onPressed: minus, child: const Text(&#39;minus&#39;))),
              Expanded(
                  child: TextButton(onPressed: add, child: const Text(&#39;add&#39;))),
            ],
          )
        ],
      ),
    );
  }
}

答案2

得分: 0

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

var defaultmonth = int.parse(DateFormat('yyyyMM').format(DateTime.now()));

@override
void minus() {
  setState(() {
    if (defaultmonth != DateTime.now().year - 1) {
      defaultmonth--;
    }
  });
}

@override
void add() {
  setState(() {
    if (defaultmonth != DateTime.now().year + 1) {
      defaultmonth++;
    }
  });
}

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

示例:

DateTime defaultmonth = DateTime.now();
var finalDefaultmonth;

@override
void minus() {
  setState(() {
    if (defaultmonth.year != DateTime.now().year - 1 || defaultmonth.month != 1) {
      DateTime newDateTime = DateTime(defaultmonth.year, defaultmonth.month - 1);
      finalDefaultmonth = DateFormat('yyyyMM').format(newDateTime);
    }
  });
}
英文:

I guess you made your code complicated:

var defaultmonth = int.parse(DateFormat(&#39;yyyyMM&#39;).format(DateTime.now()));

@override
void minus() {
  setState(() {
      if (defaultmonth != DateTime.now().year - 1) {
        defaultmonth--;
      }
  });
}

@override
void add() {
  setState(() {
      if (defaultmonth != DateTime.now().year + 1) {
        defaultmonth++;
      }
  });
}

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:

DateTime defaultmonth = DateTime.now();
var finalDefaultmonth;
@override
void minus() {
  setState(() {
    if (defaultmonth.year != DateTime.now().year - 1 || currentDateTime.month != 1) {
      DateTime newDateTime = DateTime(currentDateTime.year, currentDateTime.month - 1);
      finalDefaultmonth = DateFormat(&#39;yyyyMM&#39;).format(newDateTime);
    }
  });
}

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:

确定