`
zani
  • 浏览: 357665 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

NSString to NSDate

 
阅读更多

 

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
	[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter  setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  
 
	
    NSDate* dateFromString = [dateFormatter dateFromString:stringDate];  
	
    return dateFromString; 

 

无论环境是使用12小时制还是24小时制,yyyy-MM-dd HH:mm:ss返回都是24小时制

设置TimeZone为UTC ,这样使得转换时间不会出现8小时误差

分享到:
评论

相关推荐

    nsdate

    NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitWeekOfMonth fromDate:now toDate:laterDate options:0]; NSInteger weeksInBetween = [components weekOfMonth]; ``` ### `NSDate`...

    iphone 开发比用的NSString转化技巧

    在iOS开发中,NSString是Objective-C中用于处理文本数据的核心类,它经常需要与各种其他类型的数据进行转换,如NSArray、NSDate、int和C字符串(CString)。以下是一些关于NSString转换的重要技巧和方法: 1. **...

    iOS NSString详细操作

    NSString *path = @"/path/to/file.txt"; NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; ``` - **从URL中读取**: ```objective-c NSURL *...

    iOS NSDate中关于夏令时的坑

    NSLog(@"timeStr to date is %@ %@", timeStr, date1); ``` 这个代码块看起来很简单,但是当我们将生日换成 1986-05-04 时,date 就会转化失败,为 null。那么,什么原因导致了这个问题呢? 夏令时的坑 ---------- ...

    SecureMappingKit.zip

    SecureMappingKit 可以实现 JSON 数据和 Model 类之间安全的映射。 转换器包括: NSNumberTransformer, ...- (NSDate *)dateForKey:(id)aKey usingDateFormat:(NSString *)dateFormat;

    ios7 Foundation框架文档

    - `-(NSDate *)dateByAddingComponents:(NSDateComponents *)components toDate:(NSDate *)date options:(NSUInteger)options`: 返回通过给定日期组件修改后的日期。 - `-(NSDate *)nextWeekStartDate`: 返回下一...

    OC基础知识

    NSString *filePath = @"path/to/file"; NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; ``` 9. **日期与时间** `NSDate`代表一个特定的瞬间,`...

    swift-2-timestamp-conversions:Swift 2助手,用于将日期转换为时间戳并返回

    swift-2-timestamp-conversions Swift...1000))}// Convert the timestamp string to an NSDate objectfunc convertFromTimestamp(seconds: String) -> NSDate { let time = (seconds as NSString).doubleValue/1000.0

    iOS如何获取当前日期前后N天的时间示例代码

    NSDate *nextDay = [Calendar.current dateByAddingComponents:@{NSCalendarUnitDay : @(1)} toDate:currentDate options:0]; ``` 要获取当前日期之后N天的日期,例如7天后,我们可以使用类似的方法,只需将值传递...

    TwitterAPI封装Objectwitter-C.zip

     NSLog( @"-- success, more to come: %d, %@", morePagesToCome, response );  } pauseBlock:  ^( NSDate* nextRequestDate )   {  NSLog( @"-- rate limit exhausted, nextRequestDate:...

    IOS关于大型网站抢购、距活动结束,剩余时间倒计时的实现代码

    NSDate *endData = [NSDate dateWithTimeIntervalSince1970:endTime]; ``` 接着,我们需要一个日历对象来计算两个日期之间的差异。这里我们使用的是公历日历(`NSGregorianCalendar`),并定义了我们关心的时间单位,...

    IOS自定义日历

    3. 实现代理方法,如`- (NSString *)calendar:(JTCalendarManager *)calendar titleForDate:(NSDate *)date`返回日期标题,可以在此处理农历转换。 六、实现点击和长按手势 1. 添加手势识别器,如...

    ios开发记录

    -(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{ NSLog(@"1111"); UIImageView *imgView=(UIImageView *)context; [imgView removeFromSuperview]; } ...

    iOS 倒计时 时间转换

    在iOS中,时间戳(Unix时间戳)转换为`Date`对象,或者`NSString`(日期字符串)转换为`NSDate`(在Swift中通常使用`Date`代替)是常见的需求。这里有一个例子: ```swift // 时间戳转Date let timestamp = ...

    Objective-C词典对象的使用总结

    此外,NSDictionary中的数据成员可以是任意的Objective-C对象,但实际应用中通常是属性列表对象,如NSData、NSDate、NSNumber、NSString、NSArray或NSDictionary。当字典只包含这类对象时,可以将字典的内容持久化到...

    iphone属性列表持久化存储

    在Objective-C和Swift中,可以使用`NSDictionary`、`NSArray`、`NSData`、`NSString`、`NSNumber`、`NSDate`、`NSBoolean`等对象来创建和操作属性列表。将这些对象写入到一个.plist文件,就能在需要时读取回这些数据...

    详解IOS四种保存数据的方式

    它可以存储NSString、NSData、NSNumber、NSDate、NSArray、NSDictionary等基本类型,或者它们的子类。例如,要保存一个字符串和图片数据: ```objc NSUserDefaults *defaults = [NSUserDefaults ...

    iOS中几种数据持久化方案 --X枫林1

    - 存储:可以将NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSData、NSMutableData、NSString、NSMutableString、NSNumber、NSDate等可序列化类型的数据写入Plist文件,例如`[array writeToFile:...

Global site tag (gtag.js) - Google Analytics