-3

i want to convert nsstring to nsdate i have string have this value

08:00:00

i want to convert it so i wrote this code

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];  
NSDate *startDate = [formatter dateFromString:_startTime];
NSLog(@"%@",startDate);

_startTime have this value = 08:00:00

but startDate after i display it , it had this value

2000-01-01 06:00:00 +0000

Update I wrote this code

 NSString *dateString = _startTime;
NSDateFormatter *formatter= [[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
[formatter setDateFormat:@"HH:mm:ss"];

NSDate *startDate = [[NSDate alloc] init];
// voila!
startDate = [formatter dateFromString:dateString];

NSLog(@"%@",startDate);

i want the output be 08:00:00

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • where do you init startTime? When I run the following code it gives me the expected result. NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate *startDate = [formatter dateFromString:@"08:00:00"]; NSLog(@"%@",startDate); – Gal Marom Oct 30 '15 at 11:49
  • Please check This Answer http://stackoverflow.com/questions/33315678/date-formatter-issue-in-objc/33315709#33315709 – Ilesh P Oct 30 '15 at 11:51
  • i get it from segue but i nslog it and i works fine – ahmed alaa hamza Oct 30 '15 at 11:52
  • NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate *startDate = [formatter dateFromString:@"08:00:00"]; NSLog(@"fdhvfjfdjd%@",startDate); keep ouptut this value 2000-01-01 06:00:00 +0000 – ahmed alaa hamza Oct 30 '15 at 11:54
  • 2
    Please refer followng url [NSString to NSDate](http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again) – Vipul Oct 30 '15 at 11:54
  • Your `NSString` to `NSDate` conversion works without problems but you are then using `NSLog` without proper `NSDate` to `NSString` conversion. – Sulthan Oct 30 '15 at 12:29
  • see my answer please.... – Shahzaib Maqbool Oct 30 '15 at 12:34
  • This is an exact duplicate of [NSString to NSDate](http://stackoverflow.com/q/3917250/2792531). – nhgrif Oct 30 '15 at 12:38
  • Moreover, in order to find the *actual* question, you have to look at a comment on the check-marked answer, and to find the actual answer, you have to look in a section marked "edit" of the check-marked answer. This is an abomination of a question. – nhgrif Oct 30 '15 at 12:41

2 Answers2

0

You can't have NSDate without a date ! if you want to access the time from an NSDate you'll need to use NSDateComponents (or NSTimeInterval).

To access the time from your NSDate using NSDateComponents :

NSString *startTime = @"08:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:startDate];
NSLog(@"%d:%d:%d", [components hour], [components minute], [components second]);

I don't get what's the purpose of changing the string _startTime, do you want to compare dates ?


Edit :

NSString *startTime = @"08:00:00";
NSString *endTime = @"23:00:00";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSDate *endDate = [formatter dateFromString:endTime];

NSTimeInterval difference = [endDate timeIntervalSinceDate:startDate];

NSInteger temp = difference;

NSDate* newDate = [[NSDate alloc] init];

for (int i = 0; (i < difference && [newDate compare:startDate] == NSOrderedDescending); ++i)
{

    newDate = [startDate dateByAddingTimeInterval:(temp - i*60*10)];

    NSLog(@"%@",newDate);

}

run this and use NSDateComponents to log what you want !

enzo
  • 72
  • 1
  • 3
  • 26
0

Use this code i think this one help you..

NSString *dateStr = @"08:00:00";
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:@"HH:mm:ss"];
NSDate* mydate = [datFormatter dateFromString:dateStr];
NSLog(@"date: %@", [datFormatter stringFromDate:mydate]);

i think this is what you really want...

Shahzaib Maqbool
  • 1,479
  • 16
  • 25