Swift:在“特定日期”之前30天 [英] Swift: Get 30 days before 'Specific Date'

查看:284
本文介绍了Swift:在“特定日期”之前30天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对显示特定日期有疑问。在这种情况下,我会从我的API(06/16/2015)中获得一个随机日期,该日期总是会变化。我将其放入具有日期类型的变量名称 toDate 中。对于我的 fromDate 变量,我需要在 toDate 前30天。我阅读了但仍然无法正常工作。

I have a problem about showing specific date. Here the case, I have a random date from my API (06/16/2015) that always change. I put it into variable name toDate with Date type. I need to get 30 days before the toDate for my fromDate variable. I read this but its still not working. Thanks in Advance.

*注意:我在此控制器中没有任何DatePicker

*Note : I dont have any DatePicker in this controller

推荐答案

在这里:

let toDate = `your date object`
let fromDate = Calendar.current.date(byAdding: .month, value: -1, to: toDate)

or, 
// If you want to have exactly 30 days before
let fromDate = Calendar.current.date(byAdding: .day, value: -30, to: toDate)

这篇关于Swift:在“特定日期”之前30天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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