16

在我的 iPhone 应用程序中,我有一个事件的开始日期,例如:2013-05-17 15:00:12 +0000。我的问题是,如何将 2013-05-17 更改为今天的日期,但保持时间不变?

4

7 回答 7

38

您需要收集日期组件并修改所需的属性。

//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];

//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:[NSDate date]];

//set date components
[dateComponents setDay:17];
[dateComponents setMonth:5];
[dateComponents setYear:2013];

//save date relative from date
NSDate *date = [calendar dateFromComponents:dateComponents];

要么,或者您可以添加 1 天内的秒数来增加值:

NSDate *date = [NSDate dateWithTimeInterval:((60 * 60) * 24) sinceDate:[NSDate date]];
于 2013-06-13T10:42:05.587 回答
13

斯威夫特 3 和 IOS 10.2

    let calendar = Calendar.current

    var dateComponents: DateComponents? = calendar.dateComponents([.hour, .minute, .second], from: Date())

    dateComponents?.day = 17
    dateComponents?.month = 5
    dateComponents?.year = 2013

    let date: Date? = calendar.date(from: dateComponents!)
    print(date!)

斯威夫特 3 和 IOS 10.2

于 2017-07-21T09:38:03.193 回答
10

在我的博客上阅读更多内容https://github.com/onmyway133/blog/issues/54

今天我试图在 Swift 中将 Date 对象的年份更改为 2000。

let date = Date()

首先,我尝试过,date(bySetting:)但它不适用于过去的一年。它只是返回 nil

Calendar.current.date(bySetting: .year, value: 2000, of: date)

其次,我尝试使用dateComponents. component.year已经改变了,但是它的日历仍然返回原来的日期,很奇怪!!。无论我使用什么时区和日历,它仍然存在这个问题

var component = calendar.dateComponents(in: TimeZone.current, from: base)
component.year = year
Calendar.current.date(from: component)

最后,我试图更明确,它的工作原理

var component = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: base)
component.year = year
Calendar.current.date(from: component)
于 2017-06-09T09:47:09.190 回答
2

或迅速:

    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day], fromDate: NSDate.init())
    components.day = 17
    components.month = 5
    components.year = 2013

    let date = calendar.dateFromComponents(components)
于 2016-08-19T12:43:19.517 回答
0

您需要在凌晨 0 点找到今天的 numberOfSeconds,在凌晨 0 点找到其他日期的 numberOfSeconds。找出它们之间的区别。然后到 numberOfSeconds of 2013-05-17 15:00:12 +0000 添加这个差异。

于 2013-06-13T10:40:08.987 回答
0

为什么您不能使用 API dateWithString将开始日期更改为您想要的当前日期和时间。如果没有,那么您也可以通过获取两个 NSDate 之间的时间间隔差异来查看 NSDate 的 dateByAddingTimeInterval API

于 2013-06-13T10:40:33.360 回答
0

Swift 3.0
Get-Date 从当前日期开始具有年、月和日 -5

    //CurrentDate
    let currentDate = Date()

    let gregorianCalendar = Calendar(identifier: .gregorian)
    var components = gregorianCalendar.dateComponents([.year, .month, .day], from: currentDate)

    // Change int value which you want to minus from current Date.
    components.day = components.day!-5
    components.month = components.month!-5
    components.year = components.year!-5

    let date = gregorianCalendar.date(from: components)!
    print("Current Date = \(currentDate)")
    print("Old Date = \(date)")

斯威夫特 2.0

    //CurrentDate
    let currentDate = NSDate()

    let gregorianCalendar = NSCalendar(identifier: .gregorian)
    var components = gregorianCalendar.dateComponents([.year, .month, .day], from: currentDate)

    // Change int value which you want to minus from current Date.
    components.day = components.day!-5
    components.month = components.month!-5
    components.year = components.year!-5

    let date = gregorianCalendar.date(from: components)!
    print("Current Date = \(currentDate)")
    print("Old Date = \(date)")
于 2017-05-05T09:42:56.827 回答