获取Flutter / Dart的上个月日期 [英] Get Last Month Date In Flutter / Dart

查看:1665
本文介绍了获取Flutter / Dart的上个月日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var now = new DateTime.now(); 


var formatter = new DateFormat(’MM’);
字符串月份= formatter.format(现在);

但是如何获取上个月的日期?特别是如果当前日期是一月(01)。当我们使用操作数减(-)时(例如月份-1),我们将无法获得正确的月份。

解决方案

使用

  var prevMonth = new DateTime(date.year,date.month-1,date.day); 

  var date = new DateTime(2018,1,13); 

您将获得

  2017-12-13 

通常先转换为UTC,然后再转换为UTC在进行日期计算之前返回本地日期/时间,以避免夏令时和时区问题。


in flutter we can get current month using this

var now = new DateTime.now();
var formatter = new DateFormat('MM');
String month = formatter.format(now);

But how to get the last month date? Especially if current date is January (01). we can't get the right month when we use operand minus (-) , like month - 1.

解决方案

You can just use

var prevMonth = new DateTime(date.year, date.month - 1, date.day);

with

var date = new DateTime(2018, 1, 13);

you get

2017-12-13

It's usually a good idea to convert to UTC and then back to local date/time before doing date calculations to avoid issues with daylight saving and time zones.

这篇关于获取Flutter / Dart的上个月日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆