How can I calculate the difference between two dates?

后端 未结 9 1392
渐次进展
渐次进展 2020-11-28 02:27

How can I calculate the days between 1 Jan 2010 and (for example) 3 Feb 2010?

9条回答
  •  天命终不由人
    2020-11-28 03:29

    To find the difference, you need to get the current date and the date in the future. In the following case, I used 2 days for an example of the future date. Calculated by:

    2 days * 24 hours * 60 minutes * 60 seconds. We expect the number of seconds in 2 days to be 172,800.

    // Set the current and future date
    let now = Date()
    let nowPlus2Days = Date(timeInterval: 2*24*60*60, since: now)
    
    // Get the number of seconds between these two dates
    let secondsInterval = DateInterval(start: now, end: nowPlus2Days).duration
    
    print(secondsInterval) // 172800.0
    

提交回复
热议问题