Get time from NSDate returns nil

时光毁灭记忆、已成空白 提交于 2019-12-11 04:04:14

问题


I am trying to show time on graph and i have a full time stamp in this @"2012-08-28 18:50:24" format. when i try to get time from this date then it returns nil in NSDate.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];

in this code only if I change [formatter setDateFormat:@"HH:mm:ss"]; line to [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; then it starts working with full date and time. But i need only time


回答1:


First you have converted your string date and time in the date formate..

YourTimeStr = @"2012-08-28 18:50:24";  

  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-mm-dd HH:mm:ss";
    NSDate *DateAndTime = [formatter dateFromString:LogOutTimeSTR];

then

NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter1 setDateFormat:@"HH:mm:ss"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

    NSString *YourTimeStr =[dateFormatter1 stringFromDate:DateAndTime];



回答2:


u have string . so you must have to convert it into date first as per your string format. after that you can convert date into any format. so follow this.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *str = [formatter stringfromdate:time];
NSLog(@"%@",str);



回答3:


NSDateFormatter is used to format your NSDate to your requirements. Here you are fetching the date from a string so you have to tell the NSDateFormatter to look for the string with that format and convert it into date

NSDate contains both date and time so if you want the time only to show use dateformatter to get the time component from NSDate

iOS manages time and date together in NSDate.Actually they are supposed to work together.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];   
   // GET  Proper NSDate 
    NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];        
    //To your requirement
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *timestr=[formatter stringFromDate:time];
    NSLog(@"%@",timestr);



回答4:


Because, first you need to convert your string into NSDate in the right formate that is

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:@"2012-08-28 18:50:24"];

Now you can reformate the NSDateFormatter according to your need. Now you can get time from the

[formatter setDateFormat:@"HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:aDate];



回答5:


If you need to produce Date from String then you'll need to set the date format accordingly.

This is important that you create a NSDate object first! Only then you can separate time from whole date.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];

then

[formatter setDateFormat:@"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:time];

You can also use other string concatenation functions to remove the Date Modules like this.

However, if you have an NSDate first, then your previous code would work.

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:date];



回答6:


NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh:mm:ss  a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];    
NSLog(@"time, %@",theTime);


来源:https://stackoverflow.com/questions/12721553/get-time-from-nsdate-returns-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!