Win Coder
Win Coder

Reputation: 6756

Storing date and time separately in NSDate

So i am getting a string containing date and time in this format "2014-12-22T11:00:00+0500" Now in order to convert it into NSdate i am using

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:start_time];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString* temp = [dateFormatter stringFromDate:date];
self.eventDate = [dateFormatter dateFromString:temp];

NSDateFormatter* timeFormatter = [[NSDateFormatter alloc]init];
[timeFormatter setDateFormat:@"HH:mm:ss"];
NSString* temp2 = [timeFormatter stringFromDate:date];
self.start_time = [timeFormatter dateFromString:temp2];

Now even though the conversion is successful the problem is that eventDate also has has time after date 00:00:00. How can i remove this so that eventDate only contains date.

Conversly start_time has the time of event but also has some arbritrary reference date before that. How can i remove that so i only have time in start_time

I have searched hard and fast but haven't been able to figure out this problem. Any help would be appreciated.

Upvotes: 0

Views: 280

Answers (3)

Monikanta
Monikanta

Reputation: 307

You can try with it, it may be help you.

NSString *finalDate = @"2014-12-22T11:00:00+0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormatter dateFromString:finalDate];

//For getting Time
NSDateFormatter* df1 = [[NSDateFormatter alloc]init];
[df1 setDateFormat:@"hh:mm:ss"];
NSString *time = [df1 stringFromDate:date];
NSLog(@"time  %@ ", time);

//For getting Date
NSDateFormatter* df2 = [[NSDateFormatter alloc]init];
[df2 setDateFormat:@"yyyy-MM-dd"];
NSString *actualDate = [df2 stringFromDate:date];
NSLog(@"actualDate  %@ ", actualDate);

Upvotes: 0

regetskcob
regetskcob

Reputation: 1192

Instead of trying to store this separate, just display these dates separate. I think it could be useful sometimes to get the date completly, but i don't know your idea.

Upvotes: 1

Protothomas
Protothomas

Reputation: 79

You cannot remove either the date or the time to keep only one component. If I remember correctly NSDate object is internally just a number of seconds relative to a fixed point in time. So every NSDate contains the full date and time information.

What you probably want to do is to get the NSDateComponents you want from a NSDate object.

Upvotes: 1

Related Questions