objective-c 如何从 NSDate 中减去小时数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:57:09  来源:igfitidea点击:

How does one subtract hours from an NSDate?

iphoneobjective-cnsdate

提问by Georg Sch?lly

I would like to subtract 4 hours from a date. I read the date string into an NSDate object use the following code:

我想从日期中减去 4 小时。我使用以下代码将日期字符串读入 NSDate 对象:

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate * mydate = [dateFormatter dateFromString:[dict objectForKey:@"published"]];

What do I do next?

我接下来该怎么做?

回答by Georg Sch?lly

NSDate *newDate = [theDate dateByAddingTimeInterval:-3600*4];

Link to documentation.

链接到文档



NSDate *newDate = [[[NSDate alloc] initWithTimeInterval:-3600*4
                                              sinceDate:theDate]] autorelease];

Link to documentation.

链接到文档

回答by Chuck

NSCalendar is the general API for changing dates based on human time units. For this, you can use NSCalendar's -dateByAddingComponents:toDate:options:with a negative number of hours.

NSCalendar 是用于根据人类时间单位更改日期的通用 API。为此,您可以使用-dateByAddingComponents:toDate:options:带有负数小时数的NSCalendar 。

回答by Ben Packard

Since iOS 8 there is the more convenient dateByAddingUnit:

由于 iOS 8 有更方便的dateByAddingUnit

Swift 2.x

斯威夫特 2.x

//subtract 3 hours
let calendar = NSCalendar.autoupdatingCurrentCalendar()
newDate = calendar.dateByAddingUnit(.Hour, value: -3, toDate: originalDate, options: [])

回答by Ning

//in Swift 3
//subtract 3 hours
let calendar = NSCalendar.autoupdatingCurrent
newDate = calendar.date(byAdding:.hour, value: -3, to: originalDate)

回答by Guillaume Laurent

In Swift 4 :

在 Swift 4 中:

var baseDate = ... // something
let dateMinus4Hours = Calendar.current.date(byAdding: .hour, value: -4, to: baseDate)

don't go with 24*3600and stuff, that's asking for trouble.

不要去24*3600和东西,那是自找麻烦。

回答by Pe Gra

Here a function which might be useful as it returns the date -4 h considering that this may also change the date and the month and eventually the year. the .searchBackward option is the important part :)

这是一个可能有用的函数,因为它返回日期 -4 小时,考虑到这也可能会更改日期和月份并最终更改年份。.searchBackward 选项是重要的部分:)

public static func correctSecondComponent(date: Date, calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian))->Date {

    let hour = calendar.component(.hour, from: date)

    let e = (calendar as NSCalendar).date(byAdding: NSCalendar.Unit.hour, value: -4, to: date, options:.searchBackwards)!

    return e
}

回答by Guvener Gokce

dateFromString function returns NSDate, not NSString. you should change,

dateFromString 函数返回 NSDate,而不是 NSString。你应该改变,

NSDate * theDate = [dateFormatter dateFromString:datetemp];

NSDate * theDate = [dateFormatter dateFromString:datetemp];