NSDate of yesterday

后端 未结 6 1444
孤独总比滥情好
孤独总比滥情好 2020-11-30 01:59

How do I create an NSDate object with a custom date other than the current date? For example I would like to create a var of yesterday or of 2 days ago.

6条回答
  •  春和景丽
    2020-11-30 02:26

    This is a solution for Swift 5.1 - XCode 11

    let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
    

    Accordingly two days ago:

    let twoDaysAgo = Calendar.current.date(byAdding: .day, value: -2, to: Date())
    

提交回复
热议问题