Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i like to fetch local device time and assign to string object .

NSDate* currentDate = [NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"d MMMM , yyyy"];
self.dateSting=[[NSMutableString alloc]init];
self.dateSting = [NSMutableString stringWithFormat:@"%@" ,[dateformatter stringFromDate:currentDate]];

output received = 6 March , 2013

convert NSString to NSDate object back code below

NSDateFormatter *uu=[[NSDateFormatter alloc]init];
[uu setDateFormat:@"d MMMM , yyyy"];

NSDate *yy=[uu dateFromString:self.dateSting];

output recived=2013-03-05 18:30:00 +0000 difference coming in days.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

Well, the solution is fairly simple. NSDate always shows GMT. And from you profile, I can see that your present location is INDIA , where the local time zone is GMT +5.30hrs.

The current output you got is 2013-03-05 18:30:00 +0000 to which if you add 5.30hrs you will get 2013-03-06 00:00:00 +0000. Please note that at the end of NSDate you can see something like +0000, this shows the timezone.

This is the reason/solution for your question and if you want to know how get it solved ,the answer is here.

Hope everything is clear now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...