更改NSDate对象的年,月和日

时间:2013-06-13 10:35:20

标签: iphone ios

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

8 个答案:

答案 0 :(得分:36)

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

//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]];

答案 1 :(得分:9)

Swift 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!)
  

Swift 3& IOS 10.2

答案 2 :(得分:7)

在我的博客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)

其次,我尝试了dateComponentscomponent.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)

答案 3 :(得分:2)

或者在swift:

    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)

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

答案 6 :(得分:0)

Swift 3.0
 具有年,月和年的获取日期当前日期的第-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)")

Swift 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)")

答案 7 :(得分:0)

快速5

第一个功能只是让您从服务器字符串中获取日期。我只是为了清楚起见才添加了它。您不需要这个,仅是为了帮助我解答。

func dateFrom(_ serverDate: String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    if let date = dateFormatter.date(from: serverDate) {
        return date
    }
    return nil
}

这是从date获取时间并将其移至今天的代码。注意事项:startDate设置为午夜,因此可以将时间添加到今天。

let calendar = Calendar.current

if let date = dateFrom("2019-11-17 09:30:00") {
    let today = Date()
    let startDate = calendar.startOfDay(for: today)
    let hours = calendar.component(.hour, from: date)
    let mins = calendar.component(.minute, from: date)
    if let correctHour = calendar.date(bySetting: .hour, value: hours, of: startDate) {
        if let todayWithTime = calendar.date(bySetting: .minute, value: mins, of: correctHour) {
            // todayWithTime is today with the correct time
            // today at 9:30 am
        }
    }
}