NSDate now is 1 hr off my ios emulator - iphone

Hi I am creating an NSDate which I am using to set a Picker, but when I output the picker date to NSLog it is always an hour behind the time shown in the actual picker.
Also as a test I noticed that the code:
NSDate *now = [NSDate date]; //should give me the current date and time on my emulator
NSLog(#"Date and Time now: %#", now);
The NSLog value is always 1 hour behind the time shown on my emulator.
My question is why?
Thanks in advance :)
UPDATE --------------------------------------------------------------------------------
I did a couple of more tests and it turns out that NSDate using the NSGregorian Calendar was returning the correct time and then NSDate using the currentCalendar was off by 1 hour.
So my new question is why is that? I have checked the settings on my emulator and the device has its calendar set to Gregorian. Could this be due to a timezone difference? If so then is there a way for me to set the picker to use the same timezone as the users device?
Thanks

I think it is because your simulator time is set to default time zone (EN). NSDate is always set from your local time. When you try it on your device it will be correct.
i have this code in my project and work fine:
NSDate*now = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd.MM.yyyy"];
i create date and store it to my CoreData. Always that time will my actual time on my device.
Congrats to your new iPhone anyway. Welcome to big Apple Family! :)

Related

NSDate calculation missing 1 hour when date set manually from settings

I have a strange query...
If the date/time is set automatically in my iPhone than the time is correctly displayed as seen below...
And if the date/time is set manually with a different time zone than I am actually in (I am in India and set it to London. Works fine if set to India),it shows wrong results only for two dates calculated by using the method [self.surveyModel.creationDate dateByAddingTimeInterval:60*60*24*30] and similarly for 20 days .. see image below..
I am not setting the default time zone or local time zone for the application, and I am suing the following method to show the result...
- (NSString *)stringWithFormat:(NSString *)format {
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[outputFormatter setDateFormat:format];
NSString *timestamp_str = [outputFormatter stringFromDate:self];
[outputFormatter release];
return timestamp_str;
}
what am I doing wrong here...?
NOTE: This answer is in reply to the Comment on the Question
It seem the error is caused by regions with Daylight Savings, in this case London, England.
When the date/time is calculated automatically, the iOS DateTime library takes the DayLight Saving into account.
When dealing with the DateTime manually, which you are doing through this code [self.surveyModel.creationDate dateByAddingTimeInterval:60*60*24*30]
You are setting the DateTime without listing the DayLight option enabled. So when you take the NSTimeZone look for the DayLight savings options.

Help needed in a calendar based app

I am creating a calendar based app.
I have two dates say present date, and future date(due date), now i need to show the difference between these two dates in terms of days, hours, minutes & seconds and i want to show a timer which will continues to decrement second by second as the time increases and ultimately reaches to zero when due date comes.Basically it will be showing that how much days, hrs and seconds are left for the event.
How can I do that, please help me.
It would be a great help if i can get a similar kind of sample code.
Many Thanks in advance.
iPhone Developer
Maybe It can be helpful to you - http://blog.webscale.co.in/?p=244
Basically you want to use NSTimeInterval and NSDate to calculate the difference in time. For example:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *now = [NSDate date];
NSDate *futureDate = [dateFormat dateFromString:#"07/15/2015"];
// Now we can calculate the time interval, which really is an integer
// This is the number of seconds between the 2 dates, so with math you can calculate
// what you need. Remember, when you do dateFromString, the time is midnight 00:00:00
// however, *now is set to the current time. You may want to work on that a bit
NSTimeInterval timeDifference = [futureDate timeIntervalSinceDate:now];
// Also note, if you swap the order and do now timeIntervalSinceDate:futureDate, you
// get a negative number.

Date and timezone

In our app, we always need to pick the current Eastern time (EST or EDT).
We set the application timezone as below in the app delegate:
NSTimeZone *ESTTimeZone = [NSTimeZone timeZoneWithName:#"US/Eastern"];
[NSTimeZone setDefaultTimeZone:ESTTimeZone];
Also, for the NSDateFormatter, we set the locale as follows:
NSDateFormatter *formatForFileName = [[NSDateFormatter alloc] init];
[formatForFileName setDateFormat:#"yyyyMMdd"];
NSLocale *uslocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatForFileName setLocale:uslocale];
[uslocale release];
If my iPad is set to correct EST date and time, there is no issue. If I were to log the current date as follows:
NSLog(#"current date : %#",[formatForFileName stringFromDate:[NSDate date]]);
it will display today's date correctly.
However, if I change my iPad date to be the next day's date and log the current date, I would want the current EST date and time to be returned.
However, [NSDate date] returns me the current iPad date (the next day's date) and time and not the EST date and time.
Is there any way I can get the correct EST date and time using [NSDate date] irrespective of what the user has set on his iPad.
Thanks.
I'm not sure if I understand your question. [NSDate date] always creates a date with the date/time the OS thinks is valid at the moment, i.e. the time the device's clock is set to. If you have doubts whether this is indeed the current time, you would need to contact a trustworthy time server on the Internet and get the time from there.
[NSDate date] returns a date set to the current system date and time. So when the user changes to the next day's date on the system, [NSDate date] will return next day's date. NSDate assumes (rightfully so) that the system date is the correct date.
To display the actual EST date, you will need to query a source (a remote source) other than the system for the date that you want.

NSString to NSDate conversion problem

I am having a NSString object which I am converting to NSDate with help of NSDateFormatter. Now code works fine here with all the OS but it is creating different Output at client's add (USA region).
Here is the code that I am using.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *strConcat = #"2010-09-24 17:30:00";
NSDate *date = [formate dateFromString:strConcat];
NSLog(#"Output :%#",date);
Output at my end -------2010-09-24 17:30:00 - 0700
Output at Client end ----2010-09-25 00:30:00 GMT
Can anyone please suggest where's the problem?
Thanks
Pratik Goswami
The only output difference I notice is the time is different. If you do not explicitly set the timeZone property of the formatter it will use the system's local timezone. If you expect the times to the be the exact same from you and your client:
[formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
That would insure the output is always GMT.
You need to manually set the locale for the NSDateFormatter, so that all your users see the same formatted string. Otherwise, the formatter will use whatever locale is set for the device.
Actually, it's working correctly. The two dates are equivalent, just that one is in the US/Mountain time zone and the other is in the Greenwich time zone. (5:30pm + 7 hours = 12:30 am)
What's the problem here?
In iOS2.x and 3.x the description/datefromstring function returns:
2010-09-24 17:30:00 - 0700
in iOS 4.x it returns this:
2010-09-25 00:30:00 GMT
You could submit a bug to apple.

In Cocoa, how do I use NSTimeZone to get the system time zone offset as a string?

For PDT, I would want "-0700".
I'm getting a date in the past to determine how long ago something happened.
NSDate *then = [NSDate dateWithString:#"1976-04-01 12:34:56 -0700"]; // Note the hard-coded time zone at the end
I'll be constructing the date string elsewhere but I don't know how to access the local time zone.
I read the Apple Dates and Times Programming Topics for Cocoa as well as the NSTimeZone and NSDate Class References but it's just too hard for me to put the information together. I could really use a few lines of code just to show how it's used.
Update: While struggling with this, I was writing code using a Command Line template so I could try things quickly. I just tried my previous code on iPhone and I'm getting NSDate may not respond to '+dateWithString:' Sorry if that added to the confusion, who knew Apple would change up such a basic class.
Use NSDateFormatter to build NSDate from a string:
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *formatterDate;
formatterDate = [inputFormatter dateFromString:#"1976-04-01 12:34:56 -0700"];
NSString *dateString = [inputFormatter stringFromDate:formatterDate];
NSLog(#"date:%#", dateString);
This way you get the local time from string, for example the date specified by the string:
"1976-04-01 12:34:56 -0700"
is in time zone -0700, (I'm in time zone GMT +1000) so I get:
2009-11-17 22:13:46.480
cmdline[10593:903] date:1976-04-02
05:34:56 +1000
The time zone offset is dependent on the date in much of the world—those parts of it that use Daylight-Saving Time/Summer Time.
The only correct way is to generate the entire string from date and time-zone together. Use NSDateFormatter for this.
The best way is to probably use a simple calendar formatter
NSCalendarDate * date = [NSCalendarDate calendarDate];
[date setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"PDT"]];
NSLog([date descriptionWithCalendarFormat:#"%z"]);
which will output '-0700'
or leave out the second line if you want the current time zone of the system (not sure which you were asking for)