Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

iphone - NSDate object reciving fron NSString with different values

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...