用飞镖添加/减去月/年至今? [英] Add/Subtract months/years to date in dart?

查看:68
本文介绍了用飞镖添加/减去月/年至今?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在dart中有一个Duration类,但是不能用它加/减年或月。您如何处理此问题,我需要从日期中减去6个月。是否有类似momentjs的飞镖之类的东西?
谢谢

I saw that in dart there is a class Duration but it cant be used add/subtract years or month. How did you managed this issue, I need to subtract 6 months from an date. Is there something like momentjs for dart or something around? Thank you

推荐答案

好的,所以您可以分两个步骤进行操作,取自@zoechi (Flutter的重要贡献者):

Okay so you can do that in two steps, taken from @zoechi (a big contributor to Flutter):

定义基准时间,让我们说:

Define the base time, let us say:

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

现在,您想要新的日期:

Now, you want the new date:

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

您将得到

2017-12-13

这篇关于用飞镖添加/减去月/年至今?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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