nstimeinterval

时间:2024-05-30 10:05:23编辑:coo君

ios 怎么将yyyymmdd字符串格式转换为nstimeinterval

- (void) dateConverter{
NSString *string = @"22/04/2013 05:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm"];
NSDate *date = [[NSDate alloc] init];
// voila!
date = [dateFormatter dateFromString:string];
NSLog(@"dateFromString = %@", date);

//date to timestamp
NSTimeInterval timeInterval = [date timeIntervalSince1970];
}


如何将 NSTimeInterval 转换为 int

解决方法
直接赋值:
NSTimeInterval interval = 1002343.5432542;
NSInteger time = interval;
//time is now equal to 1002343


NSTimeInterval 是双重的所以如果你将它分配直接给 NSInteger (或 int,如果你愿意的话) 它会工作。这将切断时间精确到秒。
如果你想要舍入到最接近的秒 (而不能切断) 你可以使用圆才使分配:
NSTimeInterval interval = 1002343.5432542;
NSInteger time = round(interval);
//time is now equal to 1002344


ios怎么设置当前时间到某个时间断之间的时间差

  - (int)intervalSinceNow: (NSString *) theDate
  {
  
  NSDateFormatter *date=[[NSDateFormatter alloc] init];
  [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  NSDate *d=[date dateFromString:theDate];
  
  NSTimeInterval late=[d timeIntervalSince1970]*1;
  
  
  NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  NSTimeInterval now=[dat timeIntervalSince1970]*1;
  NSString *timeString=@"";
  
  NSTimeInterval cha=now-late;
  
  if (cha/86400>1)
  {
  timeString = [NSString stringWithFormat:@"%f", cha/86400];
  timeString = [timeString substringToIndex:timeString.length-7];
  return [timeString intValue];
  }
  return -1;
  }

  上面的例子只是计算相差了几天

  当然可以计算其他数值
  cha/3600<1 分钟
  if (cha/3600>1&&cha/86400<1) 小时

  01
  // 获取当前日期
  02
  NSDate *date = [NSDate date];
  03
  
  04
  // 打印结果: 当前时间 date = 2013-08-16 09:00:04 +0000
  05
  NSLog(@"当前时间 date = %@",date);
  06
  
  07
  // 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可
  08
  date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
  09
  
  10
  //打印结果:当前时间 往后60s的时间date = 2013-08-16 09:01:04 +0000
  11
  NSLog(@"当前时间 往后60s的时间date = %@",date);
  PS:测试时时间是下午5点,但是得到的当前时间却是上午9点,相差了8小时,是时区的问题

  解决办法:

  1
  NSTimeZone *zone = [NSTimeZone systemTimeZone];
  2
  
  3
  NSInteger interval = [zone secondsFromGMTForDate: date];
  4
  
  5
  NSDate *localDate = [date dateByAddingTimeInterval: interval];
  6
  
  7
  // 打印结果 正确当前时间 localDate = 2013-08-16 17:01:04 +0000
  8
  NSLog(@"正确当前时间 localDate = %@",localDate);


objective-c 字符串怎么转时间

NSDate类是用来表示时间的,它有一个函数dateWithNaturalLanguageString能从字符串形式的时间生成NSDate对象,但是这个函数好像在高版本不支持了。
用法:
NSDate* myDate = [NSDate dateWithNaturalLanguageString:@“3pm December 31, 2001,”];

下面是苹果网站的内容
链接地址:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/index.html#//apple_ref/occ/clm/NSDate/dateWithNaturalLanguageString:


Creates and returns an NSDate object set to the date and time specified by a given string.



Declaration


Swift

class func dateWithNaturalLanguageString(_ string: String) -> AnyObject?




Objective-C

+ (id)dateWithNaturalLanguageString:(NSString *)string






Parameters





string




A string that contains a colloquial specification of a date, such as
“last Tuesday at dinner,” “3pm December 31, 2001,” “12/31/01,” or
“31/12/01.”








Return Value

A new NSDate object set to the current date and time specified by string.



Discussion

This method supports only a limited set of colloquial phrases,
primarily in English. It may give unexpected results, and its use is
strongly discouraged. To create a date object from a string, you should
use a date formatter object instead (see NSDateFormatter and Data Formatting Guide).

In parsing the string, this method uses the date and time preferences stored in the user’s defaults database. (See dateWithNaturalLanguageString:locale: for a list of the specific items used.)





Import Statement

import Foundation




Availability

Available in OS X v10.4 and later.
Deprecated in OS X v10.10.


objective c中如何获取当前日期和时间并将其显示在一个label中?

NSDate *date=[NSDate date];//获取当前时间
NSDateFormatter *format1=[[NSDateFormatter alloc]init];
[format1 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *str1=[format1 stringFromDate:date];

UILable *lable = [[UILable alloc]init];
lable.frame = CGRectMake(20,40,280,40);
lable.textlable.text = str1;
[self.view addSubView:lable];


上一篇:完美赤壁论坛

下一篇:赛尔号洛拉菲德