xcode 如何在当前时间添加时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5440837/
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-14 20:39:36  来源:igfitidea点击:

how to add time in current time

iphonexcodeiosdatetime

提问by Shishir.bobby

I'm quite confused about this one.

我对这个很困惑。

I want to grab, current time, than according to condition, I want to add the required time, to the current time. for example.

我想抓取当前时间,而不是根据条件,我想将所需时间添加到当前时间。例如。

current time  = 06:47:10 
//or should i hv to change this format to "2011-03-26 06:47:10  GMT"

 if(a= 1 and b= min ) 
  { //add 1 min to
  current time 
  } 
  else if(a= 1 and b= hour) 
  { //add 1
   hour to current time 
  } 
 else if(a= 1 and b=week )  
 { //add 1
 week to current time  
 }

Just need to add the output of the above condition to current time.

只需要将上述条件的输出添加到当前时间即可。

Please guide me with this.

请指导我。

Regards

问候

回答by Rog

Do you mean current time, as in now?

你的意思是当前时间,就像现在一样?

If so, this will do it for you:

如果是这样,这将为您做到:

NSDate *now = [NSDate date]; // Grab current time
NSDate *newDate = [now addTimeInterval:XXX] // Add XXX seconds to *now

Where XXX is the time in seconds.

其中 XXX 是以秒为单位的时间。

回答by Matthias Bauch

You should not use #define kOneDay 86400

你不应该使用 #define kOneDay 86400

In timezones that have daylight saving, each year there is one day that only has 82800 seconds, and one day that has 90000 seconds.
And sometimes there is even a day that has 86401 seconds. (But I think the leap second is ignored by NSDateComponents too.)

在有夏令时的时区,每年有一天只有 82800 秒,一天有 90000 秒。
有时甚至有一天有 86401 秒。(但我认为 NSDateComponents 也忽略了闰秒。)

If you want to do it right you should use NSDateComponents.

如果你想做得对,你应该使用 NSDateComponents。

to add one day you use it like this:

添加一天你像这样使用它:

NSDateComponents *offset = [[[NSDateComponents alloc] init] autorelease];
[offset setDay:1];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:offset toDate:date options:0];

it is important to use setDay:1and not setHour:24.

重要的是使用setDay:1而不是setHour:24



to add two weeks and three hours you would use this

添加两周零三个小时,你会使用这个

NSDateComponents *offset = [[[NSDateComponents alloc] init] autorelease];
[offset setWeek:2];
[offset setHour:3];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:offset toDate:date options:0];

You should get the idea. Start with the biggest unit of change and work your way down to the smallest.

你应该明白了。从最大的变化单位开始,然后逐步降低到最小的单位。

Yes, it's a little bit more work than addTimeInterval:but addTimeInterval:hours*60*60is wrong if you care for days, weeks and months.

是的,如果你关心几天、几周和几个月,这比工作要多一点,addTimeInterval:但这addTimeInterval:hours*60*60是错误的。

回答by A R

'addTimeInterval:' is deprecated

You can use this now

你现在可以使用这个

mydate=[NSDate date];    
mydate = [mydate dateByAddingTimeInterval:XXX]; //XXX in seconds

回答by neeta

Swift version:

迅捷版:

let now = NSDate().timeIntervalSince1970 // current time
let timeAfterXInterval = NSDate().dateByAddingTimeInterval(XXX).timeIntervalSince1970 // time after x sec

XXX is time in sec

XXX 是以秒为单位的时间