斯威夫特:在“特定日期"之前获得 30 天 [英] Swift: Get 30 days before 'Specific Date'

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

问题描述

我在显示特定日期时遇到问题.在这种情况下,我的 API (06/16/2015) 有一个随机日期,该日期总是在变化.我把它放入变量名 toDate 与 Date 类型.我需要在 toDate 之前为我的 fromDate 变量获取 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.

我阅读了这个 但它仍然无法正常工作.注意:我在这个控制器中没有任何 DatePicker.

I read this but it's still not working. Note: I don't 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)

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

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