0

I am using NSDate to get current time and date but it is giving date and time based on GMT.Please can anyone know how to get local time?

Thanks in advance!

Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
  • look here: http://stackoverflow.com/questions/4547379/nsdate-is-not-returning-my-local-time-zone-default-time-zone-of-device – cweinberger Jan 19 '12 at 11:03

3 Answers3

1
[dateFormatter setTimeZone:systemTimeZone];

set the timezone of the date using a date formatter with the above method. It will work. Convert the date to string, set the format and timezone and convert back to date.

prince
  • 854
  • 2
  • 9
  • 36
1

NSDate has no knowledge of timezones. It always stores dates as absolute moments in time, in GMT.

Use NSDateFormatter to get a local (or any other time zone) representation.

See the pertinent programming guide: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
  • How to get current locale?Can you please give some example? – Nuzhat Zari Jan 19 '12 at 11:12
  • If you use NSDateFormatter, it will use the current locale automatically by default. If you need to know the locale you can get it using [NSLocale currentLocale] – Nick Lockwood Jan 19 '12 at 11:15
  • Sorry for misunderstanding,I want to get local current time but I need in NSDate format only not in NSString so I cannot use NSDateFormatter also.and I cannot specify the Time Zone also because based on location it should automatically convert. – Nuzhat Zari Jan 19 '12 at 11:20
  • Can you explain why you need this? As I said, NSDate is expected to be in GMT. You are fighting the framework. This smells like a design flaw (such as conflating your model and view). – Conrad Shultz Jan 19 '12 at 16:45
  • Because I want to insert the same time in database when the operation is performed not based on GMT. – Nuzhat Zari Jan 27 '12 at 09:58
  • What database takes NSDate natively - none that I know of? Or do you really mean Core Data? If you are inserting into a normal database you will need a string representation, which NSDateFormatter gets you. Alternatively (I would say better for many situations), you could store the GMT/UTC date and, separately, the UTC offset. Then add the two together in your SELECT statements. – Conrad Shultz Jan 27 '12 at 17:17
-1

Hi I found this from another site

NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

Link for the website is here

Krrish
  • 2,256
  • 18
  • 21