-1

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.

Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
  • The second log was made to NSDate and first was on NSString, The NSDate will always show in GMT 00:00. You should accept the answer by @MicRO – Anupdas Mar 06 '13 at 07:17

3 Answers3

4

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.

Community
  • 1
  • 1
DD_
  • 7,230
  • 11
  • 38
  • 59
1

Try something like this.

NSDate* currentDate = [NSDate date];
NSLog(@"%@",currentDate);
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"d MMMM hh:mm:ss, yyyy"];

NSLog(@"%@",[dateformatter stringFromDate:currentDate]);

NSDate *yy=[dateformatter dateFromString:[dateformatter stringFromDate:currentDate]];
NSLog(@"%@",yy);

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

NSLog(@"%@",[uu stringFromDate:yy]);

The difference in date and time occurs due to the GMT offset. NSLogging NSDate give you time with the offset. The offset shows the value to be added or subtracted from date. In India its +5.30. Always make sure to keep the offset details as they may change the actual date if absent. Hope this helps.

Meera
  • 1,031
  • 6
  • 25
  • always keep the general format of date with the time and its offset, when you need to show the date as formatted, use a formatter, shape it to a format as required and then show. If the offset gets clipped of, there may be inaccuracy in time and date due to the difference in time zone – Meera Mar 06 '13 at 07:10
  • i set look [dateFormatter setDateFormat:@"d MMMM hh:mm:ss, yyyy"]; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"hh:mm a"]; NSString *formattedDateString = [dateFormatter1 stringFromDate:date]; NSLog(@"yyy=%@",formattedDateString); its giving answer in Am but it should be PM – RameshRajput Mar 06 '13 at 08:58
  • It shows the correct value. When I used this code i got correct value. Please check. I am getting PM which is correct with respect to current time – Meera Mar 06 '13 at 09:04
0

dateWithString: Creates and returns an NSDate object with a date and time value specified by a given string in the international string representation format (YYYY-MM-DD HH:MM:SS ±HHMM).

  • (id)dateWithString:(NSString *)aString Parameters aString A string that specifies a date and time value in the international string representation format—YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM is a time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”). You must specify all fields of the format string, including the time zone offset, which must have a plus or minus sign prefix.
Rushabh
  • 3,208
  • 5
  • 28
  • 51